【发布时间】:2020-04-02 11:59:55
【问题描述】:
我正在尝试使用 VC++ 2017 编译此 (C++14) 代码。
#include <type_traits>
#include <limits>
struct Templ
{
template <typename T>
static constexpr int value = ( std::numeric_limits<T>::min() == 0 );
};
using type = std::conditional_t<Templ::value<unsigned>, bool, double>;
int main()
{
return 0;
}
我收到以下错误消息:
未解析的外部符号“public: static int const Templ::value”(??$value@I@Templ@@2HB) 在函数“void __cdecl Templ::`public: static int const Templ 的动态初始化器”中引用::value''(void)" (??__E??$value@I@Templ@@2HB@Templ@@YAXXZ)|
如何正确使用 conditional_t 和模板化的静态 constexpr 成员作为条件?
编辑。根据一些程序员老兄的回答,我想出了这个:
struct Templ
{
template<typename T>
struct inner
{
enum
{
value = ( std::numeric_limits<T>::min() == 0 )
};
};
};
using type = std::conditional_t<Templ::inner<unsigned>::value, bool, double>;
【问题讨论】:
-
错误消息与您显示的代码不符,因为错误消息提到
Templ::value<double>而您的代码中没有它。您显示的代码真的是minimal reproducible example,会产生您显示的错误吗? -
@Someprogrammerdude 抱歉,更新了代码和错误信息。
标签: c++ templates static conditional-statements constexpr