Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clang Compile error with default initialization [duplicate]

Tags:

c++

c++11

clang++

Consider following example:

#include <iostream>
#include <type_traits>

struct A
{
  //A() = default; // does neither compile with, nor without this line
  //A(){};         // does compile with this line
  int someVal{ 123 };


  void foobar( int )
  {
  };
};


int main()
{
    const A a;
    std::cout << "isPOD = " << std::is_pod<A>::value << std::endl;
    std::cout << "a.someVal = " <<a.someVal << std::endl;
}

See Live example

This does compile with g++ but does not compile with clang++, tried with following command: clang++ -std=c++11 -O0 main.cpp && ./a.out

Compile error from clang:

main.cpp:19:13: error: default initialization of an object of const type 'const A' requires a user-provided default constructor

I learned from This Stack Overflow Question, that non-POD classes get default constructor. This is even not necessary here because the variable has c++11-style default initialization

Why does this not for clang? Why does A() = default; not work, too?

like image 548
meddle0106 Avatar asked Mar 18 '23 04:03

meddle0106


1 Answers

This is addressed in CWG issue #253 which discusses the need for a user provided constructor for empty objects or objects whose subobjects are fully initialized (which is the case in your example).

Quoting part of the linked issue

Notes from the August, 2011 meeting:

If the implicit default constructor initializes all subobjects, no initializer should be required.

Technically it is an active issue but given that note it seems likely that it'll be resolved the way gcc chose to implement it.

Clang, on the other hand, has chosen to wait until the issue is resolved before implementing a solution.

In Clang, we're waiting for the issue to actually be resolved before we take a direction on it.

So, as it currently stands, clang is correct.

like image 190
Praetorian Avatar answered Apr 09 '23 01:04

Praetorian