-1

I have this code fragment;

struct x
{
    int thing;
};

struct y : x
{};

//works, but I need to use struct y
struct x test1 { 1 };

//Error
//"Only one level of braces is allowed on an initializer for an object of type "y"
//no suitable constructor exists to convert from "int" to "y"
struct y test { {1} }; 

//Error
//no suitable constructor exists to convert from "int" to "y"
struct y test2 { 1 };

//Error
// no suitable user-defined conversion from "x" to "y" exists
struct y test3 {test1};

//Error
// no suitable user-defined conversion from "x" to "y" exists
struct y test4 = (struct y)test1;

There are many questions on here regarding such code. From them, I discovered that the way I'm trying to initialise struct y test is valid from C++17, and works in later versions of Visual Studio 2017. I've tried to use this in Visual Studio 2019 and I get the results described in the comments.

Does anyone know how I can get VS2019 to handle initialisation of an inherited struct? I can't add a constructor to the struct because this is an abstraction of existing code I can't arbitrarily change.

Craig Graham
  • 1,161
  • 11
  • 35

1 Answers1

0

As per the comments on the OP, VS2019 defaults to C++14. To set a project to use different language standard (C++17 in this case) you have to go to project properties->C/C++->Language->C++Language Standard.

The same setting is present in VS2017.

Thanks to the commenters who helped with this.

Craig Graham
  • 1,161
  • 11
  • 35