我知道如何将模板函数作为模板参数传递,现在我正在尝试以类似的方式传递变量模板。
这是我尝试过的最小示例:
#define PASS_VARIABLE_TEMPLATE(name) [dummy=nullptr](auto&&...args) \
{return name<decltype(args)...>;}
//testing
template <typename T>
bool value = std::is_fundamental<T>::value;
template <typename Hax>
void print_bool(Hax h)
{
std::cout << h(int{}) << std::endl; // no error, wrong output
//std::cout << h(int{}, float{}) << std::endl; // error, good
}
int main()
{
print_bool(PASS_VARIABLE_TEMPLATE(value)); //prints 0 instead of 1
}
如果编译成功,为什么输出结果是错误的?
int b; int& ref = a; h(ref); //类型为int&
- xinaiz#define PASS_VARIABLE_TEMPLATE(name) [dummy=nullptr](auto&&args) { return name::type>;
}
- Sam Varshavchikstd::is_rvalue_reference
,它不会出错吗? - xinaiz