我有两个指针:pA和pB.它们指向两个大的哈希映射对象.当pB指向的哈希映射完全更新时,我想交换pB和pA.
在C++ 17中,如何快速交换它们并保证线程安全?原子?我是c ++ 17的新手.
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 ++ 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) 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 …
使用类模板时,是否可以通过推断其类型来指定特定大小?这是一个伪代码示例,代表我要询问的内容。
#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==,u8则BitCount自动==BYTE或8- 如果
Type==,u16则BitCount自动==WORD或16- 如果
Type==,u32则BitCount自动==DWORD或32- 如果
Type==,u64则BitCount自动==QWORD …
c++ templates template-specialization argument-deduction c++17
以下代码
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 ++,发现了这种奇怪的语法。这&_是什么意思,为什么作者可以将该变量用作_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 ++中声明名称变量的一种特殊方法吗?
在QT中,我试图使用一个Spinbox来表示一个以分贝为单位的功率水平,对于小于1的功率水平,它变为负无穷大。
我打电话:
this->ui->powerdbBox->setMinimum(DBL_MIN);
Run Code Online (Sandbox Code Playgroud)
但是我仍然不能输入小于0的值。我尝试将最小值设置为-1,结果相同。
为了简化我的问题,我将使用它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不接受这一点:
错误:模板专业化或离线模板定义中的无关模板参数列表