0

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,049
  • 10
  • 30

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,049
  • 10
  • 30
  • Hi Craig Graham, glad to know you've found the solution to resolve this issue! Please consider answering it and accepting it as an answer to change its status to Answered. It will also help others to solve a similar issue. See [can I answer my own question..](https://stackoverflow.com/help/self-answer), Just a reminder :) – Yujian Yao - MSFT Apr 04 '22 at 01:56
  • Done. It was resolved too soon after OP for me to be able to accept my own answer. Then forgot :) – Craig Graham Apr 04 '22 at 06:36