Basically the example says it all: assigning to a std::chrono literal - does not give an error message. I tried on gcc, clang and vc, (c++17) all these compilers accept it. Is this intended behaviour?
#include <chrono>
using namespace std::chrono_literals;
int main()
{
10ms += 1ms; // would expect error here
12ms = 10ms; // and here
// 3 = 4; // like here
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 c++20 概念和要求,并尝试创建一个检查调用有效参数的概念std::map emplace。
例如,与C++20 概念非常相似,如何定义带参数的函数的存在?但随后使用 std::map 处的 emplace 函数。
然而,这个概念并不像我期望的那样工作:在 a 上map<string, string>它使用 (int, string) 参数成功,请参见示例。这里发生了什么?
#include <iostream>
#include <concepts>
#include <map>
template<typename Tobj, typename TVal1, typename TVal2>
concept Check = requires (Tobj obj, TVal1 val1, TVal2 val2)
{
obj.emplace(val1,val2);
};
int main(int, char **)
{
std::map<std::string, std::string> map;
std::cout << Check<decltype(map), std::string, std::string>; // prints 1, as expected.
std::cout << Check<decltype(map), int, std::string>; // prints 1 but would expect 0 don't understand?! …Run Code Online (Sandbox Code Playgroud) 这可能看起来与使用 consteval 而不是 constexpr 函数的优点是什么相同的问题? 但实际上恰恰相反:
现在我们有了 C++20 consteval,在什么(现实)场景中我仍然需要/创建 constexpr 函数而不是 consteval 函数(或普通函数)?
我知道 constexpr 函数也可以使用在运行时评估的参数来调用,这与 consteval 函数不同(请参阅示例代码中的 ***),但为什么我需要它呢?我的意思是我也可以使用普通函数。
int FunNormal()
{
return 12345;
}
consteval int FunConstEval(int p)
{
return p+3;
}
constexpr int FunConstExpr(int p)
{
return p+3;
}
int main()
{
// FunConstEval(FunNormal()); // illegal
FunConstExpr(FunNormal()); // legal, but why would I ever want to do this? ***
// constexpr int a1 = FunNormal(); // illegal, obviously
// constexpr int a1 = FunConstExpr(FunNormal()); // illegal
constexpr …Run Code Online (Sandbox Code Playgroud)