【发布时间】:2019-01-14 13:37:01
【问题描述】:
我正在使用模板元编程技术,目前我只是在玩弄不同的做事方法。代码如下:
template<const int A>
struct iwrapper
{
static const int num = A;
};
template<int A, int B>
constexpr iwrapper<A+B> operator+(iwrapper<A>, iwrapper<B>)
{
return iwrapper<iwrapper<A>::num + iwrapper<B>::num>();
}
int main()
{
constexpr iwrapper<2> first;
constexpr iwrapper<4> second;
constexpr auto answer = first + second;
}
当我尝试运行它时,它给了我这个错误消息:
error: the value of 'first' is not usable in a constant expression
有人可以帮我弄清楚原因吗?谢谢。
【问题讨论】:
标签: c++ c++11 templates template-meta-programming constexpr