相关疑难解决方法(0)

为什么非静态数据成员不能成为constexpr?

这是有效的代码:

struct S {
  constexpr S(int x, int y): xVal(x), yVal(y) {}
  constexpr S(int x): xVal(x) {}
  constexpr S() {}

  const int xVal { 0 };
  const int yVal { 0 };
};
Run Code Online (Sandbox Code Playgroud)

但在这里我真的想申报xVal并 - 像yVal constexpr这样:

struct S {
  constexpr S(int x, int y): xVal(x), yVal(y) {}
  constexpr S(int x): xVal(x) {}
  constexpr S() {}

  constexpr int xVal { 0 };         // error!
  constexpr int yVal { 0 };         // error!
};
Run Code Online (Sandbox Code Playgroud)

如上所示,代码将无法编译.原因是(根据7.1.5/1),只能声明静态数据成员constexpr.但 …

c++ constexpr c++11

26
推荐指数
1
解决办法
1万
查看次数

在常量表达式中调用的`static constexpr`函数是......错误?

我有以下代码:

class MyClass
{
  static constexpr bool foo() { return true; }
  void bar() noexcept(foo()) { }    
};
Run Code Online (Sandbox Code Playgroud)

我希望,因为它foo()是一个static constexpr函数,并且因为它是在bar声明之前定义的,所以这是完全可以接受的.

但是,g++给我以下错误:

 error: ‘static constexpr bool MyClass::foo()’ called in a constant expression
Run Code Online (Sandbox Code Playgroud)

这是......不太有用,因为在常量表达式中调用函数的能力是整个点constexpr.

clang++更有帮助.除了声明参数noexcept必须是常量表达式的错误消息之外,它还说:

note: undefined function 'foo' cannot be used in a constant expression
note: declared here
static constexpr bool foo() { return true; }
                      ^
Run Code Online (Sandbox Code Playgroud)

那么......这是一个两遍编译问题吗?问题是编译器在定义任何成员函数之前是否尝试声明它们中的所有成员函数?(注意,在类的上下文之外,编译器都不会抛出错误.)这让我感到惊讶; 直观地说,我认为static constexpr成员函数不能在任何和所有常量表达式中使用,无论是在类中还是在类中.

c++ constant-expression noexcept constexpr c++11

23
推荐指数
1
解决办法
3474
查看次数

嵌套结构尽管与全局结构完全相同但仍会破坏constexpr

我遇到以下代码时遇到问题:

template<typename T>
constexpr int get(T vec) {
  return vec.get();
}

struct coord {
  constexpr int get() const { return x; }
  int x;
};

struct foo {
    struct coord2 {
      constexpr int get() const { return x; }
      int x;
    };
    constexpr static coord f = { 5 };
    constexpr static int g = get(f); // works

    constexpr static coord2 h = { 5 };
    constexpr static int i = get(h); // doesn't work
};

constexpr coord foo::f;
constexpr …
Run Code Online (Sandbox Code Playgroud)

c++ constexpr c++11

18
推荐指数
1
解决办法
375
查看次数

为什么这个constexpr静态成员函数在被调用时不被视为constexpr?

为什么这个constexpr static//! Nah注释标识的成员函数constexpr在调用时看不到?

struct Item_id
{
    enum Enum
    {
        size, position, attributes, window_rect, max_window_size, _
    };

    static constexpr int n_items_ = _;                          // OK
    constexpr auto member_n_items() const -> int { return _; }  // OK
    static constexpr auto static_n_items() -> int { return _; } // OK
    static constexpr int so_far = n_items_;                     // OK
    #ifndef OUT_OF_CLASS
        static constexpr int bah = static_n_items();            //! Nah.
    #endif
};

constexpr auto n_ids() -> int { …
Run Code Online (Sandbox Code Playgroud)

c++ constexpr

17
推荐指数
2
解决办法
1904
查看次数

在使用之前必须声明类函数/变量吗?

所以我在学习课程,偶然发现我发现的东西对我来说很尴尬.

class Nebla 
{
    public:
        int test()
        {
            printout();
            return x;
        }

        void printout()
        {
            printout2();
        }

    private:
        int x,y;
        void printout2()
        {
            cout<<"Testing my class";
        }
};
Run Code Online (Sandbox Code Playgroud)

我发现在一个类中我可以在声明它们之前使用函数(原型)

你可以看到我用printout(),printout2()decleration之前.

我也可以在声明变量之前使用变量

你可以看到我做到了return x; 在声明x之前.

为什么我可以在声明之前在类中使用函数和变量但在类之外如果我这样做,我会收到错误?

谢谢

c++ variables prototype class function

16
推荐指数
2
解决办法
4269
查看次数

如果函数在类范围内声明,constexpr不工作

我使用的是g ++ 4.8.0,它不包含早期的constexprbug.因此下面的代码工作正常:

constexpr int size() { return 5; }
int array[size()];

int main () {}
Run Code Online (Sandbox Code Playgroud)

但是,如果我将变量括在一个classas中static,那么它会给出编译器错误:

struct X {
  constexpr static int size() { return 5; }
  static const int array[size()]; 
};

int main () {}
Run Code Online (Sandbox Code Playgroud)

这是错误:

错误:数组'array'的大小不是整数常量表达式

禁止以constexpr这种方式使用还是使用另一个g ++ bug?

c++ compiler-errors g++ constexpr c++11

16
推荐指数
2
解决办法
4830
查看次数

初始化constexpr静态类成员时编译器错误

我以下列方式宣布了一堂课

class A
{
    struct B
    {
        constexpr
        B(uint8_t _a, uint8_t _b) :
            a(_a),
            b(_b)
        {}

        bool operator==(const B& rhs) const
        {
            if((a == rhs.a)&&
               (b == rhs.b))
            {
                return true;
            }
            return false;
        }

        uint8_t a;
        uint8_t b;
    };

    constexpr static B b {B(0x00, 0x00)};

};
Run Code Online (Sandbox Code Playgroud)

但是g ++说

错误:字段初始值设定项不是常量

无法弄清楚我错在哪里.

将不胜感激任何帮助!

谢谢和reagards!

constexpr c++11

7
推荐指数
2
解决办法
629
查看次数

在同一个类中使用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
查看次数

编写static const uint变量和匿名枚举变量有什么区别?

正在看一下boost asio ssl_client.cpp的例子,发现这是正确的:

enum { max_length = 1024 };
Run Code Online (Sandbox Code Playgroud)

不知道,这和之间有什么区别

namespace {
    const int max_length = 1024;
}
Run Code Online (Sandbox Code Playgroud)

要么

static const int max_length = 1024;
Run Code Online (Sandbox Code Playgroud)

或者它们绝对平等,但这只是更短?

c++ enums boost

5
推荐指数
1
解决办法
346
查看次数

无法创建静态constexpr

我有以下代码:

struct Literal
{
    int val;
    constexpr Literal(int const& val) : val(val) {}
    constexpr Literal(Literal const& rhs) : val(rhs.val) {}
};


struct Parent
{
    struct StaticObject
    {
        Literal const zero;

        constexpr StaticObject() :zero(0) {}
    };
    static constexpr StaticObject outer{};
};
Run Code Online (Sandbox Code Playgroud)

行'static constexpr StaticObject outer {};' 给我错误:

'表达式没有评估为常数'

接下来是

注意:失败是由调用未定义的函数或未声明'constexpr'的函数引起的

注意:请参阅'Parent :: StaticObject :: StaticObject'的用法

据我所知,这里使用的所有函数都是定义和声明的constexpr.我错过了什么,或者这是一个编译器错误?

c++ constexpr c++11

5
推荐指数
0
解决办法
95
查看次数

constexpr 函数调用无法编译

为什么下面的代码不能编译?

#include <stdint.h>
#include <array>

class A
{
    struct Helper
    {
        static constexpr uint64_t p2(uint8_t n)
        {
            return static_cast<uint64_t>(1) << n;
        }
    };

    using DenomArray = std::array<uint64_t, Helper::p2(5)>;
};
Run Code Online (Sandbox Code Playgroud)

使用 GCC 我得到:

error: 'static constexpr uint64_t A::Helper::p2(uint8_t)' called in a constant expression before its definition is complete
Run Code Online (Sandbox Code Playgroud)

我的理解是p2应该定义函数,因为Helper类是完全编译的。

为 x86 和 GCC 12 尝试了 MSVC 编译器版本 19.29.30040。

编辑1:

模板和非模板类的行为不同。例如下面的代码编译:

template <class T>
class A
{
private:

    struct Helper
    {
        static constexpr T p2(uint8_t n)
        {
            return …
Run Code Online (Sandbox Code Playgroud)

c++

5
推荐指数
1
解决办法
80
查看次数

为什么不能在类的另一个函数的声明中使用静态constexpr的结果?

这是我的准系统代码:

#include <iostream>
#include <array>

class cColor {
  public:
  enum eValue { k_Red, k_Green, k_Blue };
  static constexpr std::size_t NumValues() { return 3; }
  static constexpr std::array<eValue, NumValues()> Values()  { return {k_Red, k_Green, k_Blue}; }
};

int main() {
  std::cout << "NumColors=" << cColor::NumValues() << '\n';
}
Run Code Online (Sandbox Code Playgroud)

我试图声明Values()为静态constexpr,我认为我也应该能够使用,NumValues()因为它也是静态constexpr。但是,该程序无法编译,并引发以下错误:

main.cpp:8:39: error: non-type template argument is not a constant expression
  static constexpr std::array<eValue, NumValues()> Values()  { return {k_Red, k_Green, k_Blue}; }
                                      ^~~~~~~~~~~
main.cpp:8:39: note: undefined function …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

4
推荐指数
1
解决办法
74
查看次数

如何在静态constexpr类成员中使用make_tuple()?

我想用make_tuple()constexpr.它适用于全球范围.但它会为static constexpr类成员生成链接错误.

#include <iostream>
#include <tuple>

using namespace std;

class A
{
public:
    static constexpr auto z = make_tuple(5, 3.0);
};

constexpr auto tp = make_tuple(6, 3.2);

int main()
{

    cout << get<0>(tp) << " " << get<1>(tp) << endl; // OK

    cout << get<0>(A::z) << "  " << get<1>(A::z) << endl; // error: (.text+0x5a): undefined reference to `A::z'
                                                          //        (.text+0x67): undefined reference to `A::z'

}
Run Code Online (Sandbox Code Playgroud)

我已经检查了这里 make_tuple本身不是constexprc++11.我猜这不是问题.如果是,它将生成编译错误而不是链接错误. …

c++ constexpr c++11

2
推荐指数
1
解决办法
549
查看次数