标签: c++17

如何在多线程c ++ 17程序中交换两个指针?

我有两个指针:pA和pB.它们指向两个大的哈希映射对象.当pB指向的哈希映射完全更新时,我想交换pB和pA.

在C++ 17中,如何快速交换它们并保证线程安全?原子?我是c ++ 17的新手.

c++ multithreading c++17

0
推荐指数
1
解决办法
342
查看次数

为什么并行std :: for_each返回void

我正在详细阅读C++ 17.下面是std :: for_each的描述,在本书中:

在for_each的串行版本中,在C++ 17之前可用的版本>您将获得一元函数作为算法的返回值.在并行版本中不可能返回这样的对象,因为调用的顺序>是不确定的.

我仍然明白,为什么并行std :: for_each不能返回仿函数f.虽然调用的顺序是不确定的,但并行for_each块以等待调用完成.当每个并行操作完成时,返回f似乎没问题.

c++ parallel-processing c++17

0
推荐指数
1
解决办法
73
查看次数

为什么返回SFINAE转换运算符不起作用?

struct A
{
  template <typename T>
  constexpr explicit operator
  std::enable_if_t<
    std::is_same<std::decay_t<T>, int>{},
    int
  >() const noexcept
  {
    return -1;
  }
};

int main()
{
  A a;

  std::cout << int(a) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

错误是clang-7.0.1

<source>:21:16: error: no matching conversion for functional-style cast from 'A' to 'int'
  std::cout << int(a) << std::endl;
               ^~~~~
<source>:7:22: note: candidate template ignored: couldn't infer template argument 'T'
  constexpr explicit operator
                     ^
Run Code Online (Sandbox Code Playgroud)

c++ type-conversion c++17

0
推荐指数
1
解决办法
71
查看次数

包含&lt;experimental / filesystem&gt;之后,尚未声明'std :: filesystem'

我已经检查了有关c ++ 17下文件系统链接的很多问题,但仍然无法成功建立链接。我的main.cpp文件如下。

#include <experimental/filesystem>


int main(int argc, char** argv)
{
    std::string imageDirectory = "./image";;
    std::vector<std::string> imagePath;

    for (const auto& entry: std::filesystem::directory_iterator(imageDirectory))
    {
        imagePath.push_back(entry.path());
        std::cout << entry.path() << std::endl;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

CMakeLists.txt的如下。

cmake_minimum_required(VERSION 3.8 FATAL_ERROR)

project(visual_hull LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
add_library(dataIO
        STATIC
            dataIO.hpp
            dataIO.cpp)

find_package(OpenCV REQUIRED core highgui imgproc)

target_link_libraries(dataIO ${OpenCV_LIBS})

add_executable(visual_hull main.cpp)

target_link_libraries(visual_hull PUBLIC dataIO
                                         stdc++fs)
Run Code Online (Sandbox Code Playgroud)

错误如下。

/home/SENSETIME/zhangshunkang/Downloads/programming/c++/visual_hull/main.cpp: In function ‘int main(int, char**)’:
/home/SENSETIME/zhangshunkang/Downloads/programming/c++/visual_hull/main.cpp:15:31: error: ‘std::filesystem’ has not been declared
  for (const auto& …
Run Code Online (Sandbox Code Playgroud)

c++ cmake c++17

0
推荐指数
3
解决办法
1754
查看次数

Why traditional GetProcAddress to std::function is not working straightforward

As in the title I'd like to convert GetProcAddress into std::function. Yes, there are multiple solutions in stack overflow, but none actually explains why those workarounds are needed. I can't really understand the exact error message and why it happens. The sample source is simple:

#include <functional>
#include <Windows.h>

using fn_NtAllocateVirtualMemory = NTSTATUS (NTAPI*)(
        HANDLE      ProcessHandle,
        PVOID*      BaseAddress,
        ULONG_PTR   ZeroBits,
        PSIZE_T     RegionSize,
        ULONG       AllocationType,
        ULONG       Protect
    );

int main()
{
    std::function<fn_NtAllocateVirtualMemory> myFn(reinterpret_cast<fn_NtAllocateVirtualMemory>(0xDEADBABE));
}
Run Code Online (Sandbox Code Playgroud)

( https://godbolt.org/z/FhaeLA )

So, my question is …

c++ dynamic-import c++17

0
推荐指数
1
解决办法
90
查看次数

根据类型模板的类型参数自动执行其大小参数

使用类模板时,是否可以通过推断其类型来指定特定大小?这是一个伪代码示例,代表我要询问的内容。

#include <bitset>
#include <cstdint>

namespace xxx {

#define BYTE  8
#define WORD  16
#define DWORD 32
#define QWORD 64

    typedef std::uint8_t   u8;
    typedef std::uint16_t u16;
    typedef std::uint32_t u32;
    typedef std::uint64_t u64;

    template<typename Type, u16 BitCount>
    struct Register {
    };
}
Run Code Online (Sandbox Code Playgroud)

我不知道我可以使用部分专业化还是完全专业化,而且我也不知道该怎么做。我想做的是以下几点:

  • 如果Type==,u8BitCount自动== BYTE8
  • 如果Type==,u16BitCount自动== WORD16
  • 如果Type==,u32BitCount自动== DWORD32
  • 如果Type==,u64BitCount自动== QWORD …

c++ templates template-specialization argument-deduction c++17

0
推荐指数
1
解决办法
72
查看次数

无法移动std :: any

以下代码

using vptr = std::vector<std::unique_ptr<int>>;
auto m = std::unordered_map<int, std::any>{};
m.try_emplace(0, move(vptr{}));
Run Code Online (Sandbox Code Playgroud)

无法编译,抱怨使用的已删除副本构造函数unique_ptr。在模板参数中替换std::any为之后,vptr此代码将编译,因此问题显然与any

如何强制std::any移动而不是复制?

c++ move move-semantics c++17 stdany

0
推荐指数
1
解决办法
87
查看次数

使用&符号命名的变量后跟下划线是什么意思?

我正在研究C ++,发现了这种奇怪的语法。这&_是什么意思,为什么作者可以将该变量用作_1st

std::sort(open_list.begin(), open_list.end(), [] (const auto &_1st, const auto &_2st) {
  return _1st->h_value + _1st->g_value < _2st->h_value + _2st->g_value
});
Run Code Online (Sandbox Code Playgroud)

编辑1

我认为,要创建变量的引用,必须使用&运算符,后跟名称,例如:&_1st(注意它们之间的空白)。

下划线是在C ++中声明名称变量的一种特殊方法吗?

c++ reference c++17

0
推荐指数
1
解决办法
153
查看次数

尽管设置了最小值,但QDoubleSpinBox不允许负值

在QT中,我试图使用一个Spinbox来表示一个以分贝为单位的功率水平,对于小于1的功率水平,它变为负无穷大。

我打电话:

this->ui->powerdbBox->setMinimum(DBL_MIN);
Run Code Online (Sandbox Code Playgroud)

但是我仍然不能输入小于0的值。我尝试将最小值设置为-1,结果相同。

c++ qt qt5 c++17

0
推荐指数
1
解决办法
91
查看次数

如何使用模板化构造函数定义推导指南?

为了简化我的问题,我将使用它std::unique_lock作为解释工具。std :: unique_lock有一个模板参数,互斥体。但是,它的构造函数也是模板函数unique_lock(TMutex &, const chrono::duration<_Rep, _Period>&)

当一个人使用它时,可以写:

 auto lock = std::unique_lock(my_mutex, 5s);
Run Code Online (Sandbox Code Playgroud)

那么,问题是:如何为此写出演绎指南(不改变行为),怎么做?

到目前为止,我最大的尝试是:

template<typename _Mutex>
template<typename _Rep, typename _Period>
unique_lock(_Mutex &, const chrono::duration<_Rep, _Period>&) -> unique_lock<_Mutex>;
Run Code Online (Sandbox Code Playgroud)

不幸的是,clang不接受这一点:

错误:模板专业化或离线模板定义中的无关模板参数列表

c++ templates c++17 ctad

0
推荐指数
1
解决办法
91
查看次数