C++ 在派生构造函数初始化列表中初始化模板

C++ 在派生构造函数初始化列表中初始化模板,c++,templates,constructor,initializer-list,C++,Templates,Constructor,Initializer List,Foo继承std::array。可以在Foo构造函数的初始化器列表中填充数组吗 如果是这样的话,以下语法的有效替代方案是什么 // Foo is always an array of 2 ints struct Foo: std::array<int, 2> { Foo() {} Foo(const int & x, const int & y) : std::array<int, 2> { x, y } {} } //Foo始终是一个包

Foo继承std::array。可以在Foo构造函数的初始化器列表中填充数组吗

如果是这样的话,以下语法的有效替代方案是什么

// Foo is always an array of 2 ints
struct Foo: std::array<int, 2>
{
    Foo() {}
    Foo(const int & x, const int & y) : std::array<int, 2> { x, y } {}
}
//Foo始终是一个包含2个整数的数组
struct Foo:std::array
{
Foo(){}
Foo(constint&x,constint&y):std::数组{x,y}{
}
我尝试添加一对额外的大括号,它可以在g++上工作,但不能在VC2015编译器上工作:

#include <array>
#include <iostream>

struct Foo : std::array<int, 2>
{
    Foo() {}
    Foo(const int & x, const int & y) : std::array<int, 2> {{ x, y }} {}
};

int main()
{
    Foo foo(5, 12);

    std::cout << foo[0] << std::endl;
    std::cout << foo[1] << std::endl;

    system("PAUSE");
}

#包括

是的,您只需要一对额外的大括号:

struct Foo: std::array<int, 2> {
    Foo() {}
    Foo(const int & x, const int & y) : std::array<int, 2> {{ x, y }} {}
                                                           ^        ^
};
struct Foo : std::array<int, 2> {
    Foo() {}
    Foo(const int & x, const int & y) : std::array<int, 2>({ x, y }) {}
                                                          ^        ^
};
结构Foo:std::数组{ Foo(){} Foo(constint&x,constint&y):std::array{{{x,y}}{} ^ ^ };

对于VC++编译器,您需要一对括号而不是大括号:

struct Foo: std::array<int, 2> {
    Foo() {}
    Foo(const int & x, const int & y) : std::array<int, 2> {{ x, y }} {}
                                                           ^        ^
};
struct Foo : std::array<int, 2> {
    Foo() {}
    Foo(const int & x, const int & y) : std::array<int, 2>({ x, y }) {}
                                                          ^        ^
};
结构Foo:std::数组{ Foo(){} Foo(constint&x,constint&y):std::array({x,y}){ ^ ^ };
是的,您只需要一对额外的大括号:

struct Foo: std::array<int, 2> {
    Foo() {}
    Foo(const int & x, const int & y) : std::array<int, 2> {{ x, y }} {}
                                                           ^        ^
};
struct Foo : std::array<int, 2> {
    Foo() {}
    Foo(const int & x, const int & y) : std::array<int, 2>({ x, y }) {}
                                                          ^        ^
};
结构Foo:std::数组{ Foo(){} Foo(constint&x,constint&y):std::array{{{x,y}}{} ^ ^ };

对于VC++编译器,您需要一对括号而不是大括号:

struct Foo: std::array<int, 2> {
    Foo() {}
    Foo(const int & x, const int & y) : std::array<int, 2> {{ x, y }} {}
                                                           ^        ^
};
struct Foo : std::array<int, 2> {
    Foo() {}
    Foo(const int & x, const int & y) : std::array<int, 2>({ x, y }) {}
                                                          ^        ^
};
结构Foo:std::数组{ Foo(){} Foo(constint&x,constint&y):std::array({x,y}){ ^ ^ };
为什么
Foo
继承自
std::array
?在我的应用程序中,它将是一个带有GetX()SetY()函数等的点/向量类。对我来说,这比一个包含x、y、z数据成员的结构更有意义,因为它允许我删除每个维度的重复代码。如何设计东西当然取决于你。但是我会说,继承不是大多数工作的最佳工具(而且,不像C语言,大多数C++标准库不是真正设计为继承的)。虽然您可以从
std::array
继承,但请注意它没有
virtual
函数,这意味着您几乎永远不会通过
std::array
指针或引用与
Foo
交互;但这没关系,因为
std::array
的析构函数是非虚拟的,所以当你销毁对象时,你需要知道你真的有一个
Foo
。但是基类中没有任何
虚拟的
方法,我个人认为没有太多理由使用继承。最好的替代方法是什么?我试图避免使用C数组。为什么
Foo
继承自
std::array
?在我的应用程序中,它将是一个带有GetX()SetY()函数等的点/向量类。对我来说,这比带有x,y,z数据成员,因为它允许我删除每个维度的重复代码。如何设计东西当然取决于您。但是我会说,继承不是大多数工作的最佳工具(而且,不像C语言,大多数C++标准库不是真正设计为继承的)。虽然您可以从
std::array
继承,但请注意它没有
virtual
函数,这意味着您几乎永远不会通过
std::array
指针或引用与
Foo
交互;但这没关系,因为
std::array
的析构函数是非虚拟的,所以当你销毁对象时,你需要知道你真的有一个
Foo
。但是基类中没有任何
虚拟的
方法,我个人认为没有太多理由使用继承。最好的替代方法是什么?我试图避免使用C数组。不幸的是,这不起作用;请看我编辑的问题。编辑:在VS2015中谢谢!你最近的编辑很好。标记为已解决。不幸的是,这不起作用;请看我编辑的问题。编辑:在VS2015中谢谢!你最近的编辑很好。标记为已解决。