小编Fra*_*lia的帖子

constexpr唯一ID,用clang编译但不用gcc编译

早上好,伙计们!

我正在重构一个事件队列.我正在四处寻找,看看我是否可以在编译时使事件ID独特.我提出的与clang 4.0.0一起工作,但是给出了g ++ 6.3.1的编译错误.

我们的想法是使用静态成员变量的地址来唯一标识各个类型,然后使用标记从类模板生成这些唯一类型.

使用静态成员的地址作为类型id是一种相当常见的技术,但使用模板来实现它意味着要清除ODR.MSN引用此标准来表明这是一种有效的方法: 编译时常量id

我的问题是做这个constexpr.如果我删除constexpr并在运行时测试它,一切都按预期工作.但是,执行此constexpr会导致g ++中的静态断言失败,"错误:静态断言的非常量条件".

经过相当多的研究,似乎最相似的问题是:

大多数这些问题都是g ++不合格和clang ++错误输出.这是相反的.

我很难过.这是我所获得的精简版本,注释了静态断言中不能用g ++编译的内容:

template <typename tag>
struct t
{
    constexpr static char const storage{};
};
template <typename tag>
constexpr char const t<tag>::storage;

struct tag_0 {};
struct tag_1 {};

static_assert(&t<tag_0>::storage == &t<tag_0>::storage, "This always compiles.");
static_assert(&t<tag_1>::storage == &t<tag_1>::storage, "So does this.");
static_assert(&t<tag_0>::storage != &t<tag_1>::storage, "This does not compile with g++.");
static_assert(!(&t<tag_0>::storage == &t<tag_1>::storage), "Neither does this.");

constexpr auto …
Run Code Online (Sandbox Code Playgroud)

c++ gcc constexpr c++11

10
推荐指数
1
解决办法
631
查看次数

标签 统计

c++ ×1

c++11 ×1

constexpr ×1

gcc ×1