Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
OrganizationAdvent CalendarQiitadon (β)
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?


More than 5 years have passed since last update.

N4140, § 8.5.7 によるとconst修飾されたインスタンスは、コンパイラが生成したデフォルトコンストラクタで初期化することはできないらしい。

If a program calls for the default initialization of an object of a const-qualified type T, T shall be a class type with a user-provided default constructor.
- N4140, § 8.5.7

struct Data {
    Data() = default;
    Data(int) {}

int main() {
    Data d1(1); // OK
    Data d2; // OK
    Data const d3(1); // OK
    Data const d4; // Error
    return 0;
$ clang++ -std=gnu++14 -stdlib=libc++ -Wall -Wextra main.cpp
main.cpp:10:16: error: default initialization of an object of const type
'const Data' requires a user-provided default constructor
    Data const d4; // Error
1 error generated.
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away