1

The following compiles with the Microsoft C++ compiler (VS 15.5.6 --std=c++14) instead of producing the expected error.

#include <string>

template<class type>
class Foo {
public:
    Foo(type x, std::string y) 
        : x(x)
        , y(y)
    {}

    type x;
    std::string y;
};

template<class type>
class Bar {
public:
    // this line should produce a compile error
    Foo<type> f = { 1, 2, 3 };
};


int main() {
  Bar<double> b;

  // b.x is some random number
  // b.y can't be inspected (probably garbage)
}

Compiling with g++6.3 produces

could not convert '{1, 2, 3}' from '<brace-enclosed initializer list>' to 'Foo<std::__cxx11::basic_string<char> >'

Is this a compiler bug in Visual studio?

AndyMcoy
  • 179
  • 1
  • 3
  • 13
  • In `clang` I get " error: non-aggregate type 'Foo' cannot be initialized with an initializer list" so I think your code is only working in Visual C++ by some quirk of their compiler. I'm not sure this is a bug, but it could be a peculiar interpretation on their part. What values does `f` get populated with? – tadman May 10 '18 at 15:27
  • First question: What is the documented behaviour here for Visual C++? – tadman May 10 '18 at 15:29
  • 1
    Not reproducible. Tried to compile with Visual Studio. Got "error C2661: 'Foo::Foo': no overloaded function takes 3 arguments" – AnT stands with Russia May 10 '18 at 15:30
  • @FrançoisAndrieux I think the question is why does that code compile when that's the case, it shouldn't. – tadman May 10 '18 at 15:31
  • 2
    Fails to compile with version 15.7.0. Looks like a bug if it is actually compiling for you. – NathanOliver May 10 '18 at 15:32
  • 1
    I see it in v15.5.6 as well, looks like an update will take care of it. – Hans Passant May 10 '18 at 15:37
  • Does /W4 output any warnings? – ricco19 May 10 '18 at 15:44
  • No warnings on /W4. I'll accept 'Fixed in 15.7.0' as an answer after I upgrade and try it myself. – AndyMcoy May 10 '18 at 15:48

0 Answers0