我正在尝试编写某种预处理器怪物来制作简单的 ctors。
这编译为g++ -std=c++17:
template<typename T>
struct foo{
T x;
foo(T _x):x(_x){}
};
auto x=foo(3);
Run Code Online (Sandbox Code Playgroud)
但是怪物很难知道 的类型x,所以我尝试了这个:
template<typename T>
struct foo{
T x;
foo(decltype(x) _x):x(_x){}
};
auto x=foo(3);
Run Code Online (Sandbox Code Playgroud)
哪个失败(class template argument deduction failed)。但decltype(x)只是T无论如何,对不对?那么为什么代码示例不等效呢?
例子:
struct c{
void operator=(bool){}
operator bool(){
return false;
}
c&operator=(const c&)=delete;
};
void f(bool){}
int main(){
c a,b;
f(b); //works fine
a=b; //g++ -std=c++17 says: error: use of deleted function ‘c& c::operator=(const c&)’
}
Run Code Online (Sandbox Code Playgroud)
为什么f(b)调用转换b为boolmatchf的类型但a=b坚持不转换?