GCC和clang之间的constexpr差异 [英] constexpr differences between GCC and clang

查看:89
本文介绍了GCC和clang之间的constexpr差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下在GCC 9中进行编译,但在clang 10中不进行编译,我想知道两个编译器中的哪一个符合标准:

The following compiles in GCC 9 but not in clang 10 and I'm wondering which of the two compilers is standard conforming:

template<typename T>
struct A {
  static const T s;
  static const T v;
};

template<typename T>
constexpr const T A<T>::s = T(1);

template<typename T>
constexpr const T A<T>::v = A<T>::s;

int main(int, char**) {
  constexpr auto a = A<double>::v;
  return 0;
}

这只是一个更大问题的最小例子,这就是为什么字段 s v 被明确声明为 const ,但定义为 constexpr ,这是有意的。

This is intended to be a minimal example of a bigger issue which is why the fields s and v are explicitly declared as const but are defined as constexpr, this is intentional.

GCC正确地编译了该代码还是clang正确地拒绝了它?

Is GCC correct to compile that code or is clang correct to reject it?

推荐答案

仅需要编译器处理 static const 整型变量和枚举类型为 constexpr (如果它们使用常量表达式初始化)。这样可以在将 constexpr 添加到语言之前,将它们用作数组长度。

Compilers are only required to treat static const variables of integral and enum types as constexpr if they are initialize with a constant expression. This made it possible to use them as array lengths before constexpr was added to the language.

这篇关于GCC和clang之间的constexpr差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆