我有以下 C++14 代码:
template<typename T>
struct Test{
static constexpr auto something{T::foo()};
};
这很好,只要 T::foo()
也是一个 constexpr
。
现在我知道 something
是 ODR 使用的,所以我需要提供命名空间声明。我应该使用什么语法?
template<typename T>
constexpr auto Test<T>::something;
不起作用。 谢谢!
最佳答案
通过 using
定义的类型名怎么样?
template <typename T>
struct Test
{
using someType = decltype(T::foo());
static constexpr someType something{T::foo()};
};
template<typename T>
constexpr typename Test<T>::someType Test<T>::something;
关于c++14 static constexpr auto 与 odr 用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54590141/