似乎C++ 17在所有参数都有默认值时添加了在模板类上删除"<>"的能力(就像我们已经能够长时间使用函数一样),例如:
template<int LENGTH = 1>
struct MyStruct{ int arr[LENGTH]; };
int main()
{
MyStruct<2> a;
MyStruct<> b; // old way to use defaults
MyStruct c; // new way to use defaults
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,使用别名模板时,似乎该功能不再有效,例如:
template<int LENGTH = 1>
struct MyStruct{ int arr[LENGTH]; };
template<int LENGTH = 1>
using MyAlias = MyStruct<LENGTH>;
int main()
{
MyAlias<2> a;
MyAlias<> b; // old way still works
MyAlias c; // new way doesn't compile:
// gcc 7.3: missing template arguments before 'c' …Run Code Online (Sandbox Code Playgroud) 我有一个关于 C++ 中的下标运算符、重载和继承的问题。我很确定,如果您有一个具有多个函数重载的父类,那么子类可能只覆盖其中一个函数并继承其余函数。这似乎不适用于下标运算符。(我做了一个错误的假设。它确实与任何其他函数没有什么不同。)考虑以下代码:
struct A {};
struct B {};
struct Parent
{
virtual ~Parent() {}
virtual int operator[](A index) { return -1; }
virtual int operator[](B index) { return -2; }
};
struct Child : public Parent
{
virtual int operator[](B index) override { return -3; }
};
int main()
{
// error: no match for 'operator[]' (operand types are 'Child' and 'A')
return Child()[A()];
}
Run Code Online (Sandbox Code Playgroud)
我希望它使用来自父级的下标运算符而不是导致错误。是否可以从父级继承一些重载的下标运算符并覆盖其他下标运算符?如果没有,有没有比这样做更好的解决方案:
struct Child : public Parent
{
virtual int operator[](B index) override …Run Code Online (Sandbox Code Playgroud)