【发布时间】:2017-03-01 23:02:19
【问题描述】:
像下面这样的类可以利用template argument deduction for class templates吗?
template <int I>
struct Number
{
/* appropriate constructor here */
};
“利用”我的意思是,有没有办法(隐式或显式)推导出 I 的值?示例用法如下:
Number a(3); // a's type is Number<3>
【问题讨论】:
-
您似乎希望模板为您创建一个构造函数——这是不可能的。
-
@NeilButterworth 不是我的意图,只是如果知道如何拼写特定的构造函数,我就不会问这个问题。我会编辑澄清
-
int模板参数是编译时的东西 - 在运行时将它传递给构造函数是没有意义的。 -
@NeilButterworth 阅读了我对布赖恩回答的评论
-
是的,但为什么不简单地
Number <3> a;?