相关疑难解决方法(0)

如何告诉static_assert constexpr函数参数是const?

我有一个constexpr函数,看起来像这样:

constexpr int foo(int bar)
{
   static_assert(bar>arbitrary_number, "Use a lower number please");

   return something_const;
}
Run Code Online (Sandbox Code Playgroud)

但是,使用GCC 4.6.3进行编译时不断告诉我

错误:'bar'不能出现在常量表达式中

我试过类似的东西

constexpr int foo(constexpr const int bar)
{
   static_assert(bar>arbitrary_number, "Use a lower number please");

   return something_const;
}
Run Code Online (Sandbox Code Playgroud)

但constexpr不能用于函数参数.

是否有一些简单的方法告诉编译器bar始终是编译时常量?

c++ constexpr c++11

9
推荐指数
2
解决办法
3888
查看次数

在同一个类中使用constexpr作为模板参数时出错

如果我尝试编译以下C++ 0x代码,我收到一个错误:

template<int n> struct foo { };

struct bar {
    static constexpr int number() { return 256; }

    void function(foo<number()> &);
};
Run Code Online (Sandbox Code Playgroud)

使用gcc 4.6.1,错误消息是:

test.cc:6:27: error: ‘static constexpr int bar::number()’ used before its definition
test.cc:6:28: note: in template argument for type ‘int’
Run Code Online (Sandbox Code Playgroud)

使用clang 2.8,错误消息是:

test.cc:6:20: error: non-type template argument of type 'int' is not an integral
      constant expression
        void function(foo<number()> &);
                          ^~~~~~~~
1 error generated.
Run Code Online (Sandbox Code Playgroud)

如果我将constexpr函数移动到基类,它适用于gcc,并在clang上给出相同的错误消息:

template<int n> struct foo { };

struct base {
    static constexpr …
Run Code Online (Sandbox Code Playgroud)

c++ compiler-errors constexpr c++11

6
推荐指数
1
解决办法
3047
查看次数

标签 统计

c++ ×2

c++11 ×2

constexpr ×2

compiler-errors ×1