我正在学习SFINAE(替换失败不是)我在网站上找到了一个例子,
template<typename T>
class is_class {
typedef char yes[1];
typedef char no [2];
template<typename C> static yes& test(int C::*); // What is C::*?
template<typename C> static no& test(...);
public:
static bool const value = sizeof(test<T>(0)) == sizeof(yes);
};
Run Code Online (Sandbox Code Playgroud)
我int C::*在第5行发现了一个新的签名.起初我以为它是,operator*但我想这不是真的.请告诉我它是什么.
我最近开始学习C++.我在std :: forward上有一个关于左值引用和右值引用的问题.根据我的理解,以下代码中的Func(mc)假设由于模板参数推导规则而调用Func(T&t).并且应该在函数内部使用消息"复制构造函数"调用MyClass的复制构造函数.但是,当我运行程序时,我无法得到它.我检查了带有右值引用的std :: forwad,并且复制构造函数在其他行中运行良好.请帮我理解代码中发生的事情.如果我容易犯错误或误解,我很抱歉花时间.非常感谢你.
class MyClass {
public:
MyClass() { printf("constructor.\n"); };
MyClass(MyClass&) { printf("copy constructor.\n"); };
MyClass(const MyClass&) { printf("const copy constructor.\n"); };
MyClass(MyClass&&) { printf("move constructor.\n"); };
int val = 3;
};
template <typename T>
void Func(T&& t) {
T new_t_(std::forward<T>(t));
new_t_.val *= 2;
};
main() {
MyClass mc;
Func(mc); // lvalue <- Func(T&)
Func(MyClass()); // rsvalue <- Func(T&&)
printf("mc.val=%d\n", mc.val); // this is for check
MyClass mc2(mc); // this is for check of copy constructor
} …Run Code Online (Sandbox Code Playgroud) 我遇到了一个decltype()带有两个参数作为模板函数的返回值类型:
template<class C, class F>
auto test(C c, F f) -> decltype((void)(c.*f)(), void()) { }
Run Code Online (Sandbox Code Playgroud)
有人知道第二个参数是什么void()吗?非常感谢你.