【发布时间】:2019-08-27 15:08:19
【问题描述】:
考虑以下两段带有CRTP 模式的代码:
template <typename Derived>
struct Base1 {
int baz(typename Derived::value_type) {
return 42;
}
};
struct Foo1 : Base1<Foo1> {
using value_type = int;
};
template <typename Derived>
struct Base2 {
auto baz() {
return typename Derived::value_type {};
}
};
struct Foo2 : Base2<Foo2> {
using value_type = int;
};
第一个fails to compile,第二个compiles。我的直觉说他们应该要么编译要么都不编译。现在,如果我们将Base2 中的auto 替换为显式类型:
template <typename Derived>
struct Base3 {
typename Derived::value_type baz() {
return typename Derived::value_type {};
}
};
struct Foo3 : Base3<Foo3> {
using value_type = int;
};
它no longer compiles;但我真的不明白这里有什么大区别。怎么回事?
注意:这出现在 David S. Hollman 的闪电演讲中,Thoughts on Curiously Recurring Template Pattern,在 C++-Now 2019 中。
【问题讨论】:
-
@PicaudVincent:谢谢,看起来很有趣。太糟糕了,该博客上的字体/背景对比很差。