好.我有Win10 x64和这个非常简单的代码:
int main() {
std::conditional_variable cv;
std::mutex m;
std::unique_lock<std::mutex> lock(m);
while(1) {
cv.wait_for(lock, 1000ms, [](){return false;});
std::cout << "!" << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
是.代码就像我期望的那样工作; 表明 '!' 每一秒.但如果我改变当地时间(例如减去1小时/分钟),它就永远存在.如果我cv.wait_for(...)用WinAPI 替换Sleep(1000)它工作正常.WinAPI也SleepConditionVariableCS可以正常工作.
我试过这个:
cv.wait_until(lock, std::chrono::steady_clock::now() + 1000ms, [](){return false;});
Run Code Online (Sandbox Code Playgroud)
它给了我相同的结果.
BTW:调用的行为一样std::this_thread::sleep_for,std::this_thread::sleep_until都是一样的.
所以问题是:是否可以使用std::condition_variable预期的行为?或者我只需要用我自己的CONDITION_VARIABLE包装器替换它?
老实说,我不知道如何开始搜索我试图解决的问题的解决方案。可能已经有解决方案了。所以任务来了。
我有一个实际上是带有 2 个参数的模板的类:
template <typename F, typename S>
class trans {...};
Run Code Online (Sandbox Code Playgroud)
我还有另一个类,它包含一系列这些“反式”类,如元组(示例):
class holder {
using chain_type = std::tuple<trans<std::string, int>,
trans<int, float>,
trans<float, std::string>,
trans<std::string, json>>;
};
Run Code Online (Sandbox Code Playgroud)
并且可以看出,“trans”的第二个参数与下一个参数相同。连锁,链条:
std::string -> int -> float -> std::string -> json.
Run Code Online (Sandbox Code Playgroud)
我想要什么......我想要一些方法来制作这样的链条:
std::string -> int -> float -> std::string -> json.
Run Code Online (Sandbox Code Playgroud)
是否可以?我对可变参数模板不是很熟悉,并且很少使用它们。
假设我们有一个简单的概念,例如:
template <typename T>
concept MyConcept = requires {
T::value == 42;
};
Run Code Online (Sandbox Code Playgroud)
根据我的理解,这个概念是说,如果代码T::value == 42对于类型 T 有效,我就会通过。所以value必须是静态成员,对吗?
我有一个struct
struct Test { int value = 0; }
Run Code Online (Sandbox Code Playgroud)
和下一个模板函数
template <MyConcept T>
void call() { /*...*/ }
Run Code Online (Sandbox Code Playgroud)
当我尝试这样做时:
int main()
{
call<Test>();
}
Run Code Online (Sandbox Code Playgroud)
有用!
问题是:它为什么有效? Test::value == 42不是该类型的有效代码Test。
我找到了一种修复它的方法,例如:
template <typename T>
concept MyConcept = requires {
*(&T::value) == 42;
};
Run Code Online (Sandbox Code Playgroud)
它按预期“工作”:
<source>:6:20: note: the required expression '((* & T::value) == 42)' is …Run Code Online (Sandbox Code Playgroud)