有人可以解释为什么现代 C++ 构造函数语法中的对象名称后面有一个分号吗?例如,在构造函数中... rd; {};

-3 c++ syntax constructor

大约 20 年前,我曾经写过一些 c++。我需要重新做一些工作,但事情已经改变了。尽管在 Stroustrup 的 c++ 书中的第四版中进行了搜索,但我确实不理解一些新语法。我正在使用 Apple clang 版本 15.0.0 (clang-1500.1.0.2.5) 目标:arm64-apple-darwin23.3.0 线程模型:posix。在构造函数中,我希望使用 rd{}; 之类的语法来初始化对象。我不明白为什么编译器想要在对象名称后面加一个分号,例如 rd;{};

以下代码编译并运行 - 我只是不明白注释中指出的部分。

#include <iostream>
#include <random>

using namespace std;

class normRand {

public:

    random_device rd;
    mt19937 gen;
    normal_distribution<double>  dist;

    normRand() {
          // The constructor is meant to initialise the Mersenne twister
          // random number generator with a seed from some device and then
          // use this generator to randomly select values from a standard
          // normal distribution.
      
          // Why is there a semicolon after the object name?
          // The compiler says that the object names are expressions - why?
          
          /* I DO NOT UNDERSTAND! */
          rd; {};
          gen;  {rd();};
          dist; {0.0, 1.0;};       
          /* ******************** */
    }
    
    double val() {
      return dist(gen);
    }

};

normRand myNormRand;

int main() {
    cout << myNormRand.val();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Nat*_*ica 6

编译器会抱怨这样做,rd{};因为这不是您可以在构造函数主体中执行的操作。 rd已经被初始化,因此尝试重新初始化它是一个错误。

您应该做的是使用类成员初始值设定项列表来初始化成员,如下所示:

class normRand {

public:

    random_device rd;
    mt19937 gen;
    normal_distribution<double>  dist;

    normRand() : rd{}, gen{rd}, dist{0.0, 1.0} {}
    
    double val() {
      return dist(gen);
    }

};
Run Code Online (Sandbox Code Playgroud)

编译的原因rd; {};是因为rd;是一个 id 表达式并且仅求值为 的值rd,并且{}是一个空代码块,后跟 的空表达式;