xml*_*lmx 9 c++ templates type-traits template-meta-programming c++17
#include <type_traits>
template<typename T>
struct remove_cvref
{
using type = std::remove_cv_t<
std::remove_reference_t<T>>;
};
template<typename T>
using remove_cvref_t =
typename remove_cvref<T>::type;
template<typename T>
constexpr bool isCc = std::is_copy_constructible_v<
remove_cvref_t<T>>;
class A final
{
public:
A() = default;
template<typename T, bool = isCc<T>> // error
A(T&&) {}
};
A f()
{
A a;
return a;
}
int main()
{}
Run Code Online (Sandbox Code Playgroud)
错误消息:
error : constexpr variable 'isCc<const A &>' must be initialized by a constant expression
1>main.cpp(14): note: in instantiation of variable template specialization 'isCc<const A &>' requested here
1>main.cpp(15): note: in instantiation of default argument for 'A<const A &>' required here
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\type_traits(847): note: while substituting deduced template arguments into function template 'A' [with T = const A &, b1 = (no value)]
1>main.cpp(8): note: in instantiation of variable template specialization 'std::is_copy_constructible_v<A>' requested here
1>main.cpp(14): note: in instantiation of variable template specialization 'isCc<A>' requested here
1>main.cpp(15): note: in instantiation of default argument for 'A<A>' required here
1>main.cpp(21): note: while substituting deduced template arguments into function template 'A' [with T = A, b1 = (no value)]
Run Code Online (Sandbox Code Playgroud)
但是,如果我A按如下方式更改类:
class A final
{
public:
A() = default;
template<typename T,
bool = std::is_copy_constructible_v<
remove_cvref_t<T>>> // ok
A(T&&) {}
};
Run Code Online (Sandbox Code Playgroud)
一切都好.
为什么C++的variable template表现不如预期?
在点在哪里std::is_copy_constructible_v<A>被实例化,定义后立即即isCc,A是不完整的,而std::is_copy_constructible_v要求其模板参数是完整的.
这段代码是否应该起作用仍然是一个起草问题:核心语言问题287,所以一些编译器接受你的代码而其他编译器拒绝它是合理的.
在没有的版本中isCc,即使在std::is_copy_constructible_v<A>实例化的时候,A也是完整的1,所以所有编译器都乐于接受代码.
1标准中的相关规则:
一个完整的类的上下文是一个
- 功能体,
- 默认参数,
- noexcept-specifier([except.spec]),
- 合同条件,或
- 默认成员初始化程序
在类的成员规范内......
......在完整的课堂环境中,课程被认为是完整的......