1
int main() {
  struct WorkItem {
    int node;
    unsigned predecessorIndex = 0;
  };  

  auto x = WorkItem { 0 };

  return 0;
}

This code compiles fine with Clang, but not with GCC:

source_file.cpp: In function ‘int main()’:

source_file.cpp:9:25: error: no matching function for call to ‘main()::WorkItem::WorkItem()’ auto x = WorkItem { 0 }; ^

source_file.cpp:9:25: note: candidates are:

source_file.cpp:4:10: note: main()::WorkItem::WorkItem() struct WorkItem { ^

source_file.cpp:4:10: note: candidate expects 0 arguments, 1 provided

source_file.cpp:4:10: note: constexpr main()::WorkItem::WorkItem(const main()::WorkItem&)

source_file.cpp:4:10: note: no known conversion for argument 1 from ‘int’ to ‘const main()::WorkItem&’

source_file.cpp:4:10: note: constexpr main()::WorkItem::WorkItem(main()::WorkItem&&)

source_file.cpp:4:10: note: no known conversion for argument 1 from ‘int’ to ‘main()::WorkItem&&’

Or MSVC:

source_file.cpp(9): error C2440: 'initializing': cannot convert from 'initializer list' to 'main::WorkItem'

source_file.cpp(9): note: No constructor could take the source type, or constructor overload resolution was ambiguous

Is Clang incorrectly compiling this code, or are MSVC and GCC wrong, from a standards point of view? Also, why does removing the = 0 allow GCC and MSVC to compile? E.g.

int main() {
  struct WorkItem {
    int node;
    unsigned predecessorIndex = 0;
  };  

  auto x = WorkItem { 0 };

  return 0;
}

GCC version: 4.9.3 Clang version: 3.7.0 MSVC version: 19.00.24215.1

1
  • Show the entire compile command, including all options. The -std= option is especially going to affect the result. Also, that's a very old version of g++. Commented Dec 27, 2016 at 15:49

1 Answer 1

6

GCC version: 4.9.3

Aggregate initialization with default member initializers is a C++14 feature which GCC does not support until GCC 5.x.

MSVC version: 19.00.23506

I believe this is Update 1 of VC 2015. Aggregate initialization with default member initializers is a C++14 feature that VC doesn't support until 2017.

Sign up to request clarification or add additional context in comments.

4 Comments

I got the MSVC version wrong: 19.00.24215.1 is the correct one
In other words, in C++11 WorkItem is not an aggregate, but in C++14 it is.
@HBellamy: That's 2015 Update 3 (it'd be much easier if you just listed the year and update number. Using versions makes me have to go look up what they mean). It's still not VC 2017, which is when VC implemented this feature.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.