use*_*460 5 c++ templates constexpr template-argument-deduction c++17
是否有可能推导出c ++ 17函数的模板值(而不是类型)?
函数foo:
template<int I>
int foo()
{
return (I);
}
Run Code Online (Sandbox Code Playgroud)
可以通过以下方式调用
foo<5>();
Run Code Online (Sandbox Code Playgroud)
并将返回5.
模板类型可以通过函数参数的类型推断出来.是否可以以某种方式对模板值执行相同操作?例如:
template<int I = x>
int bar(const int x)
{
return (I);
}
Run Code Online (Sandbox Code Playgroud)
这显然不会起作用(因为x
在声明之前需要一个),但是可能会有一些C++ 17技巧可以实现这一点吗?
我想用它来设置常量表达式函数参数.
你想要的只能通过(ab)使用类型推导来进行整数推导.注意:
template<int x>
struct integer_value {};
template<int x>
void test(integer_value<x> val)
{
//x can be used here.
}
Run Code Online (Sandbox Code Playgroud)
当然,您必须使用test(integer_value<4>{})
类似的东西来调用它.
归档时间: |
|
查看次数: |
398 次 |
最近记录: |