专业化结构中类型后的等号有什么用?
What for is equals sign after a type in specialization struct?
我遇到过这样的代码:
template <int = 42>
struct Foo{
int x; };
有道理吗,还是一派胡言?
这等于
template <int I = 42>
struct Foo {
int x;
}
所以一个 类型 int
的非类型模板参数默认为 42
但由于没有人使用 I
,它对 struct
.
的内容没有任何影响
将 Foo
声明为
时
Foo foo {};
这将导致 Foo<T>
成为 Foo<42>
但也可以声明
Foo<1> foo {};
或类似的 template struct
的不同实例。这是否有意义确实取决于您的代码,但它是 有效的 C++ 语法.
我遇到过这样的代码:
template <int = 42>
struct Foo{
int x; };
有道理吗,还是一派胡言?
这等于
template <int I = 42>
struct Foo {
int x;
}
所以一个 类型 int
的非类型模板参数默认为 42
但由于没有人使用 I
,它对 struct
.
将 Foo
声明为
Foo foo {};
这将导致 Foo<T>
成为 Foo<42>
但也可以声明
Foo<1> foo {};
或类似的 template struct
的不同实例。这是否有意义确实取决于您的代码,但它是 有效的 C++ 语法.