locked
explicit ctor vector::vector(size_type) implicitly called by the compiler RRS feed

  • Question

  • In Visual Studio Professional 2013 Update 4 the following code compiles without errors:

    class Foo
    {
    std::vector<int> v = 666;
    };

    Why? I don't understand it. 666 is an int. An int cannot be converted to std::vector because there is no matching converting constructor (the candidate std::vector::vector(size_type) does not match because it's declared explicit). Is this not true? Even IntelliSense complains:
    IntelliSense: no suitable constructor exists to convert from "int" to "std::vector<int, std::allocator<int>>"

    At run-time this code eventually calls std::vector::vector(size_type) with 666. How can that be? Is this really correct? If so then non-static data member initialization must be somehow different from "normal" initialization because std::vector<int> v = 666; in a function or at global or namespace scope (correctly) yields the following error:
    error C2440: 'initializing' : cannot convert from 'int' to 'std::vector<int,std::allocator<_Ty>>'

    I already posted this as a bug twice:

    https://connect.microsoft.com/VisualStudio/feedback/details/956918/bug-in-visual-c-compiler-nov-2013-ctp-with-non-static-data-member-braced-init-list-initialization

    https://connect.microsoft.com/VisualStudio/feedback/details/970093/explicit-ctor-vector-vector-size-type-implicitly-called-by-the-compiler

    It was finally closed as fixed. But it's still present. Please tell me why.

    Or is this not a bug?



    • Edited by powerchord Friday, November 14, 2014 9:12 AM
    Friday, November 14, 2014 8:41 AM

Answers

  • It was finally closed as fixed. But it's still present. Please tell me why.

    This was fixed in Visual Studio 2015 Preview.

    • Proposed as answer by John Boncek Monday, November 17, 2014 2:53 PM
    • Marked as answer by May Wang - MSFT Tuesday, November 25, 2014 2:37 AM
    Monday, November 17, 2014 3:43 AM
  • This was fixed in Visual Studio 2015 Preview.

    The compiler must generate C2440 (or something similar). IntelliSense alone is not sufficient here because it does not prevent the compiler from generating bad code.

    Does the compiler generate an error?

    Here you go:

    Tuesday, November 18, 2014 10:44 AM

All replies

  • On 14/11/2014 09:41, powerchord wrote:

    In Visual Studio Professional 2013 Update 4 the following code compiles without errors:

    class Foo
    {
    std::vector<int> v = 666;
    };


    Why? I don't understand it. 666 is an int. An int cannot be converted to std::vector because there is no matching converting constructor (the candidate std::vector::vector(size_type) does*not* match because it's declared explicit). Is this not true? Even IntelliSense complains:
    IntelliSense: no suitable constructor exists to convert from "int" to "std::vector<int, std::allocator<int>>"

    It seems a compiler bug.

    I tried compiling with g++, and I got the following error:
     > error: could not convert '666' from 'int' to 'std::vector<int>'
     > std::vector<int> v = 666;
     Giovanni

    Friday, November 14, 2014 11:25 AM
  • It seems there is a general issue with the keyword "explicit" (and not with the number of the beast). It simply gets lost in NSDMI.

    For example:
    struct Test
    {
    explicit Test(void *) {}
    };

    Test t1 = nullptr; // C2440
    void f()
    {
    Test t2 = nullptr; // C2440
    }
    struct Foo
    {
    Test t3 = nullptr; // OK!!!
    };

    I think Microsoft must fix this. It can't be ignored.

    • Edited by powerchord Friday, November 14, 2014 1:39 PM
    Friday, November 14, 2014 1:34 PM
  • It was finally closed as fixed. But it's still present. Please tell me why.

    This was fixed in Visual Studio 2015 Preview.

    • Proposed as answer by John Boncek Monday, November 17, 2014 2:53 PM
    • Marked as answer by May Wang - MSFT Tuesday, November 25, 2014 2:37 AM
    Monday, November 17, 2014 3:43 AM
  • This was fixed in Visual Studio 2015 Preview.

    The compiler must generate C2440 (or something similar). IntelliSense alone is not sufficient here because it does not prevent the compiler from generating bad code.

    Does the compiler generate an error?

    Tuesday, November 18, 2014 8:54 AM
  • This was fixed in Visual Studio 2015 Preview.

    The compiler must generate C2440 (or something similar). IntelliSense alone is not sufficient here because it does not prevent the compiler from generating bad code.

    Does the compiler generate an error?

    Here you go:

    Tuesday, November 18, 2014 10:44 AM
  • This was fixed in Visual Studio 2015 Preview.

    The compiler must generate C2440 (or something similar). IntelliSense alone is not sufficient here because it does not prevent the compiler from generating bad code.

    Does the compiler generate an error?

    Here you go:


    But this is not NSDMI.
    Tuesday, November 18, 2014 12:46 PM
  • Hmm guess it's an issue with NSDMI after all then.
    Tuesday, November 18, 2014 12:55 PM
  • May Wang, you marked this issue as answered. But unfortunately I must use VS2013. VS2015 is not available yet and no developer in his right mind would use a preview version for production code.

    Now the question before me is: will this bug be fixed in the version that developers (including me) are currently working with, that is VS2013?

    Would be nice to get an answer from you.

    Thanks.

    • Edited by powerchord Wednesday, December 3, 2014 8:10 AM
    Wednesday, December 3, 2014 8:09 AM