c++14 static constexpr auto 与 odr 用法

标签 c++ templates c++14 constexpr auto

我有以下 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/

相关文章:

java - 动态生成变量名

c++ - 将 c 样式字符串传递给函数 C++

c++ - C++ 的 vector 或映射或 HashMap ?

templates - Odoo <t t-call ="website.layout"> qWebException

c++ - 带有 msvc140 的通用 lambda

c++ - 从指针中提取 2 个字节 (u_short)

c++ - 对从静态实例引用的类模板的静态成员的 undefined reference

Drupal - 查找模板的一般方法?

c++ - 非模板类中的模板化 friend 类,其中 friend 也使用该类

c++ - g++ 4.9 拒绝 C++14 中的有效聚合初始化