Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"default constructor cannot be referenced" in Visual Studio 2015

I am facing a really weird error message in Visual Studio 2015. The following stripped down code:

struct A
{
    A(int val = 0)
    :
        x(val)
    {}

    int x = 0;
};

struct B: A
{
    static int y;
};

int B::y = 1;

struct C: B
{

};

int main()
{
    C c;
    return 0;
}

compiles without any problem on Clang. However Visual Studio 2015 IntelliSense gives the following error message:

the default constructor of "C" cannot be referenced -- it is a deleted function

Am I missing something in my code, or this is a bug in Visual Studio?


UPDATE

Based on the comments and answers here I have opened a bug report on Microsoft Connect.

like image 821
plasmacel Avatar asked Nov 29 '15 00:11

plasmacel


1 Answers

This is an Intellisense bug. Both clang and gcc accept this code, also webcompiler an online Visual c++ compiler accepts this code.

The draft C++14 standard section 12.1 [class.ctor] says a defaulted default constructor for a class is deleted if:

  • X is a union-like class that has a variant member with a non-trivial default constructor,
  • any non-static data member with no brace-or-equal-initializer is of reference type,
  • any non-variant non-static data member of const-qualified type (or array thereof) with no brace-orequal- initializer does not have a user-provided default constructor,
  • X is a union and all of its variant members are of const-qualified type (or array thereof),
  • X is a non-union class and all members of any anonymous union member are of const-qualified type (or array thereof),
  • any potentially constructed subobject, except for a non-static data member with a brace-or-equalinitializer, has class type M (or array thereof) and either M has no default constructor or overload resolution (13.3) as applied to M’s default constructor results in an ambiguity or in a function that is deleted or inaccessible from the defaulted default constructor, or
  • any potentially constructed subobject has a type with a destructor that is deleted or inaccessible from the defaulted default constructor.

none of which applies here.

Update

In the bug report filed by the OP the response was:

Thank you for reporting this issue. Fix should be available in the next update to Visual Studio 2015.

like image 132
Shafik Yaghmour Avatar answered Sep 29 '22 18:09

Shafik Yaghmour