C++ initializer_list 参数 - 它们可以有默认值吗?

标签 c++ visual-c++

以下代码在 Visual Studio 2013(v12.0.30501.00 更新 2)中导致 C1001 内部错误 - 我应该期望它工作吗? (可下载 here )

我希望能够在没有 vals 参数的情况下调用 func 函数,并使用默认值 {10.0}

感谢任何帮助!

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;
}

最佳答案

是的,initializer_list 参数可以有默认值,但 MSVC 2013 x86 编译器中存在一个错误,这意味着它们不受支持 (http://connect.microsoft.com/VisualStudio/Feedback/details/925540)。

关于C++ initializer_list 参数 - 它们可以有默认值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24862339/

相关文章:

c++ - 用于标记未初始化变量的 VS 2008 编译器选项

visual-c++ - 使用 Visual C++ Express 2010 时找不到 atlbase.h

c++ - 链接使用不同版本的 GCC 构建的目标文件

c++ - 静态变量的 Constexpr 构造函数导致动态初始化

C++ lambda 代表

c++ - 在 GPU 中使用八叉树组织 3D 体数据

c++ - vc++ mfc中串行编程的一些问题

c++ - cygwin 在 g++4.9.2 中支持 C++11

c++ - 全局加载 Vulkan 函数

c++ - 使用模运算的正确方法