小编b3n*_*nj1的帖子

在static_assert()中在编译时显示整数

这是我正在尝试做的简化版本

enum First
{
    a,
    b,
    c,
    nbElementFirstEnum,
};
enum Second
{
    a,
    b,
    c,
    nbElementSecondEnum,
};

static_assert(
    First::nbElementFirstEnum == Second::nbElementSecondEnum,
    "Not the same number of element in the enums.");
/*static_assert(  
    First::nbElementFirstEnum == Second::nbElementSecondEnum, 
    "Not the same number of element in the enums." + First::nbElementFirstEnum + " " + Second::nbElementSecondEnum);*/
Run Code Online (Sandbox Code Playgroud)

但是我希望能够在断言消息中打印First :: nbElementFirstEnum和Second :: nbElementSecondEnum的值(就像在注释版本中显然不起作用).我尝试使用"#"进行宏连接.我还尝试使用可变参数模板,使用%10检索每个数字并将"0"字符添加到检索到的值,但我得到的只是constexpr char [].

所以我的问题是如何让我的枚举值以字符串文字打印.

可能重复:

C++ 11 static_assert:参数化错误消息

在static_assert输出中集成类型名称?

最有趣的话题是: 在编译时打印sizeof(T) 但是我不希望有警告或退出代码来知道值.

c++ macros integer static-assert c++11

11
推荐指数
3
解决办法
7400
查看次数

将代码从gcc移植到clang

嗨,我正在尝试使我的代码在clang 3.2-9下编译,这是我无法编译的简化示例:

template<template <class>class Derived, typename Type>
class Foo
{
    public:
        Foo(){}
};

template<typename Type>
class Bar
    : public Foo<Bar, Type>
{
    public:
        Bar()
            : Foo<Bar, Type>()
        {}
};

int main()
{
    Bar<int> toto;
}
Run Code Online (Sandbox Code Playgroud)

这是clang告诉我的错误:

test.cpp:14:19: error: template argument for template template parameter must be a class template
            : Foo<Bar, Type>()
                  ^
test.cpp:14:15: error: expected class member or base class name
            : Foo<Bar, Type>()
              ^
test.cpp:14:15: error: expected '{' or ','
3 errors generated.
Run Code Online (Sandbox Code Playgroud)

它在gcc 4.7.2下编译没有任何问题.我无法使用正确的语法使其在clang下工作.请有人帮帮我,我有点卡住了......

c++ parameters templates clang c++11

3
推荐指数
1
解决办法
829
查看次数

标签 统计

c++ ×2

c++11 ×2

clang ×1

integer ×1

macros ×1

parameters ×1

static-assert ×1

templates ×1