c++11 initializer lists as default argument - by XiaoGuang Zeng

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.


5
0
Sign in
to vote
ID 809243 Comments
Status Closed Workarounds
Type Bug Repros 1
Opened 11/21/2013 6:17:09 AM
Access Restriction Public

Description

When use c++11 initializer list to specify default argument value of std::string type, wrong constructor is called.
Sign in to post a comment.
Posted by Microsoft on 2/7/2014 at 10:55 AM
Hi:
    Thanks for reporting the issue.
    A fix for this issue has been checked into the compiler sources. The fix should show up in the future release of Visual C++.

Xiang Fan
Visual C++ Team
Posted by Shafik Yaghmour on 1/10/2014 at 7:10 PM
It looks like this is broken for the general case, so whenever the default argument is an initializer list, it looks for the wrong constructor. The following code:

#include <iostream>

struct test {
test () { std::cout << "test ()" << std::endl ; }
test (int) { std::cout << "test (int)" << std::endl ; }
};

void func( test const &s = {} )
{
}

int main()
{
    test s = {} ;
    func() ;
}

should produce this results:

test ()
test ()

but instead produces:

test ()
test (int)

and this code:

#include <iostream>
#include <initializer_list>

struct test {
test () { std::cout << "test ()" << std::endl ; };

test (int) { std::cout << "test (int)" << std::endl ; };
test ( std::initializer_list<int>) { std::cout << "test (initializer_list<int>)" << std::endl ; } ;
};

void func( test const &s = {0} )
{
}

int main()
{
    test s = {0} ;
    func() ;
}

instead of producing:

test (initializer_list<int>)
test (initializer_list<int>)

generates the following error:

error C2440: 'default argument' : cannot convert from 'initializer-list' to 'const test &'
    Reason: cannot convert from 'initializer-list' to 'const test'
    No constructor could take the source type, or constructor overload resolution was ambiguous
Posted by Microsoft on 11/21/2013 at 11:59 PM
Thank you for submitting feedback on Visual Studio and .NET Framework. Your issue has been routed to the appropriate VS development team for investigation. We will contact you if we require any additional information.
Posted by Microsoft on 11/21/2013 at 6:50 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)