Partha Lal
Partha Lal

Reputation: 541

C++ initializer_list parameters - can they have default values?

The following code causes a C1001 internal error in Visual Studio 2013 (v12.0.30501.00 Update 2) - should I expect it to work? (downloadable here)

I was expecting to be able to call the func function without a vals argument and have the default of {10.0} used.

Any help appreciated!

C.hpp:

#include <string>
#include <initializer_list>
#pragma once

class C {
public:
    void func(std::string str, std::initializer_list<double> vals = { 10.0 });
};

C.cpp:

#include "stdafx.h"
#include "C.hpp"
#include <iostream>

using namespace std;

void C::func(std::string str, std::initializer_list<double> vals){
    cout << "str is " << str << endl;
    for (double v : vals){
        cout << v << endl;
    }
}

initializer_list_default_parameter.cpp:

#include "stdafx.h"
#include "C.hpp"

int _tmain(int argc, _TCHAR* argv[])
{
    C inst;
    inst.func("name"); // this line causes a C1001 error with MSVC 2013
    //inst.func("name", { 4.3 }); this line compiles
    return 0;
}

Upvotes: 3

Views: 1131

Answers (1)

Partha Lal
Partha Lal

Reputation: 541

Yes, initializer_list parameters can have default values, but there's a bug in the MSVC 2013 x86 compiler meaning they're not supported (http://connect.microsoft.com/VisualStudio/Feedback/details/925540).

Upvotes: 1

Related Questions