16

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.

plasmacel
  • 8,183
  • 7
  • 53
  • 101

1 Answers1

8

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.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • Do you know whether `cl` (the actual C++ compiler used by Visual Studio) accepts the code? That's somewhat more relevant to a VS user than gcc and clang. – Ben Voigt Nov 29 '15 at 01:09
  • 1
    @BenVoigt [webcompiler accepts the code](http://webcompiler.cloudapp.net/) unfortunately I can't make a live link. I wish they would add it, all the rest of them let you make live links and it a useful feature. – Shafik Yaghmour Nov 29 '15 at 01:09
  • 1
    Couldn't figure out why Visual Studio was upset, but I was declaring private data members as Const. Moving the Const fields to global scope resolved the issue – Luminaire Apr 20 '16 at 22:38
  • yes removing the const from variables in class somehow solves this issue – serup Jun 28 '18 at 13:50