【问题标题】:static_assert and Intel C++ compilerstatic_assert 和英特尔 C++ 编译器
【发布时间】:2016-03-03 05:39:24
【问题描述】:

阅读cppreference.com

静态断言声明可能出现在块范围内(作为块 声明)和在类主体内(作为成员声明)

好的,现在我有以下代码:

struct foo_t
{
    static constexpr std::size_t maxAlignment()
    {
        // This is just a sample; I removed real code from this method.
        return std::max(alignof(__m128), __alignof(__m256));
    }

    static_assert(0 == ((maxAlignment() -1) & maxAlignment()), "some message");
};

MSVC 2015 和 Intel C++ 16.0.2 均未编译此代码(前者显示“错误 C2131:表达式未计算为常量”,后者显示“函数调用必须在常量表达式中具有常量值”错误和点在static_assert 中调用maxAlignment)。

但 MSVC 2015 Update 1 编译以下代码,而 Intel C++ 16.0.2 不会

template <typename T, std::size_t Alignment>
struct foo_t
{
    static constexpr std::size_t maxAlignment()
    {
        return std::max(std::alignment_of<T>::value, Alignment);
    }

    static_assert(0 == ((maxAlignment() -1) & maxAlignment()), "some message");
};

foo_t<__m128, 16> foo {};
// foo_t<__m128, 33> boo {};  // here `static_assert` will stop compilation

(因此,当 static_asserttemplate 类主体中时,MSVC 可以处理它)

但是两个编译器都成功编译了以下代码(static_assert 在类主体之外;它出现在块范围内):

struct foo_t
{
    static constexpr std::size_t maxAlignment()
    {
        return std::max(alignof(__m128), __alignof(__m256));
    }
};

static_assert(0 == ((foo_t::maxAlignment() -1) & foo_t::maxAlignment()), "some message");

我的问题是:我错过了什么还是英特尔 C++ 编译器的错误?

【问题讨论】:

  • “MSVC 2015 成功编译了上面的代码。” 不在我的电脑上:“错误 C2131:表达式未计算为常量”我在 Intel 和 GCC 上得到了相同的结果。
  • @ZDF 我有“更新 1”——这可能会导致 MSVC 2015 编译该代码(当 static_assert 在类体内时)。
  • 编译器现在到处都是constexpr 支持;我们在 GCC 和 MSVC 中遇到过代码生成错误。
  • 鲁斯兰,我不明白。我的意思是我从 all 三个编译器中得到了相同的错误。 @Crashworks 很难相信所有三个编译器都以同样的方式出错。也许是关于一些晦涩的 ISO 声明。
  • MSVC2015-U1 编译您更新的代码而其他人不编译的原因可能是this: "...我们已经修复了大约 45 个与 constexpr 使用相关的错误。 . 我们在这方面的待办事项中还有大约 30 个错误..."

标签: c++ visual-c++ static-assert icc


【解决方案1】:

如果我没记错的话,constexpr 函数在完全定义之前不能使用,并且类成员 constexpr 函数在定义类之前是未定义的,这意味着您不能在类中使用 constexpr 成员函数- scope static_assert 定义了这个函数。

但是你可以使这个函数独立(无论如何它已经是静态的)并且它会完成这项工作。它应该到处编译。

【讨论】:

  • 函数是static,是的,但它取决于模板的参数。
  • @RuslanGaripov,独立函数也可能有模板参数
  • 但是 ICC 无法编译模板类这一事实——这又如何呢?这是编译器错误吗?
  • @RuslanGaripov,我会说是的——这是一个错误,因为所有其他编译器都会编译它。但可以肯定的是,我会咨询标准
猜你喜欢
  • 2021-08-08
  • 1970-01-01
  • 2014-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-22
  • 2013-11-11
  • 2010-11-21
相关资源
最近更新 更多