0
votes
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++.Ben Voigt

1 Answers

5
votes

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.