是否可以使用 CRTP 在 C++ 中访问 child 类型?
Is it possible to access child types in c++ using CRTP?
我有这个玩具示例,
template <typename TChild>
struct Base {
template <typename T>
using Foo = typename TChild::template B<T>;
};
struct Child : Base<Child> {
template <typename T>
using B = T;
};
using Bar = Child::Foo<int>;
编译失败。目的是我有一个 parent class 提供基于 child class 成员的类型计算。 child class 是通过 CRTP 提供的。然而,行
using Foo = typename TChild::template B<T>;
编译失败:
<source>: In instantiation of 'struct Base<Child>':
<source>:16:16: required from here
<source>:13:11: error: invalid use of incomplete type 'struct Child'
13 | using Foo = typename TChild::template B<T>;
| ^~~
<source>:16:8: note: forward declaration of 'struct Child'
16 | struct Child : Base<Child> {
| ^~~~~
我是不是天真地期望这样的构造能起作用?
处的失败代码
这是否符合您的意思?
#include <type_traits>
template <typename TChild>
struct Base {
template <typename T>
static auto constexpr getFoo() {
return typename TChild::template B<T>{};
}
};
struct Child : Base<Child> {
template <typename T>
using B = T;
};
using Bar = decltype(Child::getFoo<int>());
static_assert(std::is_same_v<Bar, int>);
我基本上用模板静态函数替换了模板别名,该函数 returns 是您希望模板别名编码的类型的默认构造对象,因此 decltype
-ing 其结果应该给你想要的类型。
下面给出了与要求完全相同的结果。为什么在以前的技术不起作用时它应该起作用仍然是个谜。
#include <type_traits>
#include <string>
template <typename TChild>
struct Base {
template <typename T>
static auto foo(){
return typename TChild::template B<T>();
}
template <typename T>
using Foo = std::decay_t<decltype(foo<T>())>;
};
struct Child : Base<Child> {
template <typename T>
using B = T;
};
static_assert(std::is_same<Child::B<int>,int>::value,"");
static_assert(std::is_same<Child::B<std::string>,std::string>::value,"");
让我post另一种方法:
template<typename TChild, class T>
struct GetB {
using Type = typename TChild::template B<T>;
};
template<typename TChild>
struct Base {
template<typename T>
using Foo = typename GetB<TChild, T>::Type;
};
struct Child : Base<Child> {
template<typename T>
using B = T;
};
我没有语言律师类型的解释为什么这有效,但它应该与具有额外的间接级别有关。当编译器看到
using Foo = typename TChild::template B<T>;
它可以(并且将会)在此时检查并抱怨使用了不完整的类型。但是,当我们将对 B<T>
的访问包装到函数或结构中时,
using Foo = typename GetB<TChild, T>::Type;
然后我们现在不访问 TChild
的内部,我们只是使用它的名称,这很好。
CRTP 的问题是派生的 class 在 CRTP 定义中不完整,因此您不能使用它的 using
。
在
template <typename T>
using Foo = typename TChild::template B<T>;
- 需要
TChild
的完整类型,因为 ::
。
TChild
是模板 T
的 非依赖性 ,因此应该进行第一遍检查(但失败)
您可能会使用外部特征来处理这种情况
template <typename C, typename T>
struct Traits_For_Base
{
using type = typename C::template B<T>;
};
template <typename TChild>
struct Base {
template <typename T>
using Foo = typename Traits_For_Base<TChild, T>::type;
};
Traits_For_Base<TChild, T>
是 T
的 dependent,因此无需进行首次通过检查。
并且,通过第二遍检查(T
的 dependent),Child
将完成。
或者你可以改变你的别名使类型依赖于 class Base
:
的模板参数
template <typename TChild>
struct Base {
template <typename T,
typename C = TChild,
std::enable_if_t<std::is_same_v<C, TChild>, int> = 0> // To avoid hijack
using Foo = typename C::template B<T>;
};
C
是模板的dependent,第一阶段无法检查
问题在于,嵌套模板的实例化需要完整的封闭类型class和模板B的声明:
template <typename TChild>
struct Base {
// TChild should be complete at the moment of this declaration
// template B should be declared at this moment.
template <typename T>
using Foo = typename TChild::template B<T>;
};
实例化 Base
struct Child : Base<Child>
/* TChild = Child at this moment is incomplete */
{
template <typename T>
using B = T;
/* Point where template B begins to exist */
}; /* point where Child is complete */
这些规则是语言设计的,它们的目标是避免强制编译器在代码中多次来回传递,可能是无限递归,以实际实例化您的意思。弱类型的解释器语言往往没有这样的问题,因为它们可以在以后“纠正”自己。
案例一
静态函数解决方案之所以有效,是因为没有完成类型声明。您已经声明了一个实际上具有全局作用域的函数模板,但尚未创建具体的函数或类型。
struct Base {
template <typename T>
static auto constexpr getFoo() {
return typename TChild::template B<T>{};
}
};
struct Child : Base<Child> {
template <typename T>
using B = T;
/* At this point we can instantiate Child::getFoo<int>()*/
}; /* Child is complete now */
此时 Child::getFoo<T>
的实例化是可能的,但它只需要 return 类型的函数。
using Bar = decltype(Child::getFoo<int>());
您可以在声明 B
之后将此声明放入 Child
,因为此时 B<int>
将完成。你仍然不能在 Base
中声明它
案例二
您的解决方案声明了另一个模板 Foo,它没有在 Base
中实例化它。此模板未明确依赖于 TChild
,但需要 foo()
的原型在实例化时存在。
template <typename TChild>
struct Base {
template <typename T>
static auto foo(){
return typename TChild::template B<T>();
}
// Foo is a template
template <typename T>
using Foo = std::decay_t<decltype(foo<T>())>;
};
实例化发生在您将使用 Base::Foo<T>
的位置,而您实际上并没有。该声明在您的解决方案中是空操作。在 B
声明之后使用它是合法的。您不能在 Base
内或声明 B
之前的任何地方使用它。
现在,如果您 实际上 需要在 Base
中使用 B
的实例怎么办? trait class 解决方案来了:
案例三
特征可以是专门用于子 classes 或具体 classes 的模板,这是一种设计选择。特征作为 CRTP 基 class 的基 class 并且是一种混合形式。它的作用是为 CRTP 提供有用的声明。最灵活的特征命名可能的解决方案之一:
template <typename TChild, template<class> typename Trait>
struct Base : public Trait<TChild> {
// Trait<TChild>:: tells compiler that Foo is dependant on TChild
// and is declared in base class Trait. As compiler had reached this
// point, the substitution was successful and thus Trait is complete
using Foo = typename Trait<TChild>::template B<int>;
// Foo is assumed to be a complete type, we can use it here!
Foo make_foo() { return Foo{}; }
};
// Declaring trait template in this case.
template <typename T> struct ChildTrait;
// And specializing
template <>
struct ChildTrait<struct Child> {
template <typename T>
using B = T;
};
struct Child : Base<Child,ChildTrait> {
using Bar = typename Base::Foo;
};
static_assert(std::is_same<Child::B<int>,int>::value,"");
static_assert(std::is_same<Child::B<std::string>,std::string>::value,"");
这里的想法是 Trait<TChild> = ChildTrait<Child>
必须并且可以是 Base
中的完整 class,否则我们无法从中导出 Base
。稍作修改(省略 using
、static_assert
、typename
的使用)这将在 C++98 中编译,因为它不需要 decltype
。这种方法被一些标准组件的实现所使用,例如通过 std:: stream
s.
特征可以描述具体的存储类型、分配器等。重要的是生成的具体类型没有任何关系,但有一个在 Base
中声明的共享接口。
我有这个玩具示例,
template <typename TChild>
struct Base {
template <typename T>
using Foo = typename TChild::template B<T>;
};
struct Child : Base<Child> {
template <typename T>
using B = T;
};
using Bar = Child::Foo<int>;
编译失败。目的是我有一个 parent class 提供基于 child class 成员的类型计算。 child class 是通过 CRTP 提供的。然而,行
using Foo = typename TChild::template B<T>;
编译失败:
<source>: In instantiation of 'struct Base<Child>':
<source>:16:16: required from here
<source>:13:11: error: invalid use of incomplete type 'struct Child'
13 | using Foo = typename TChild::template B<T>;
| ^~~
<source>:16:8: note: forward declaration of 'struct Child'
16 | struct Child : Base<Child> {
| ^~~~~
我是不是天真地期望这样的构造能起作用?
处的失败代码这是否符合您的意思?
#include <type_traits>
template <typename TChild>
struct Base {
template <typename T>
static auto constexpr getFoo() {
return typename TChild::template B<T>{};
}
};
struct Child : Base<Child> {
template <typename T>
using B = T;
};
using Bar = decltype(Child::getFoo<int>());
static_assert(std::is_same_v<Bar, int>);
我基本上用模板静态函数替换了模板别名,该函数 returns 是您希望模板别名编码的类型的默认构造对象,因此 decltype
-ing 其结果应该给你想要的类型。
下面给出了与要求完全相同的结果。为什么在以前的技术不起作用时它应该起作用仍然是个谜。
#include <type_traits>
#include <string>
template <typename TChild>
struct Base {
template <typename T>
static auto foo(){
return typename TChild::template B<T>();
}
template <typename T>
using Foo = std::decay_t<decltype(foo<T>())>;
};
struct Child : Base<Child> {
template <typename T>
using B = T;
};
static_assert(std::is_same<Child::B<int>,int>::value,"");
static_assert(std::is_same<Child::B<std::string>,std::string>::value,"");
让我post另一种方法:
template<typename TChild, class T>
struct GetB {
using Type = typename TChild::template B<T>;
};
template<typename TChild>
struct Base {
template<typename T>
using Foo = typename GetB<TChild, T>::Type;
};
struct Child : Base<Child> {
template<typename T>
using B = T;
};
我没有语言律师类型的解释为什么这有效,但它应该与具有额外的间接级别有关。当编译器看到
using Foo = typename TChild::template B<T>;
它可以(并且将会)在此时检查并抱怨使用了不完整的类型。但是,当我们将对 B<T>
的访问包装到函数或结构中时,
using Foo = typename GetB<TChild, T>::Type;
然后我们现在不访问 TChild
的内部,我们只是使用它的名称,这很好。
CRTP 的问题是派生的 class 在 CRTP 定义中不完整,因此您不能使用它的 using
。
在
template <typename T>
using Foo = typename TChild::template B<T>;
- 需要
TChild
的完整类型,因为::
。 TChild
是模板T
的 非依赖性 ,因此应该进行第一遍检查(但失败)
您可能会使用外部特征来处理这种情况
template <typename C, typename T>
struct Traits_For_Base
{
using type = typename C::template B<T>;
};
template <typename TChild>
struct Base {
template <typename T>
using Foo = typename Traits_For_Base<TChild, T>::type;
};
Traits_For_Base<TChild, T>
是 T
的 dependent,因此无需进行首次通过检查。
并且,通过第二遍检查(T
的 dependent),Child
将完成。
或者你可以改变你的别名使类型依赖于 class Base
:
template <typename TChild>
struct Base {
template <typename T,
typename C = TChild,
std::enable_if_t<std::is_same_v<C, TChild>, int> = 0> // To avoid hijack
using Foo = typename C::template B<T>;
};
C
是模板的dependent,第一阶段无法检查
问题在于,嵌套模板的实例化需要完整的封闭类型class和模板B的声明:
template <typename TChild>
struct Base {
// TChild should be complete at the moment of this declaration
// template B should be declared at this moment.
template <typename T>
using Foo = typename TChild::template B<T>;
};
实例化 Base
struct Child : Base<Child>
/* TChild = Child at this moment is incomplete */
{
template <typename T>
using B = T;
/* Point where template B begins to exist */
}; /* point where Child is complete */
这些规则是语言设计的,它们的目标是避免强制编译器在代码中多次来回传递,可能是无限递归,以实际实例化您的意思。弱类型的解释器语言往往没有这样的问题,因为它们可以在以后“纠正”自己。
案例一 静态函数解决方案之所以有效,是因为没有完成类型声明。您已经声明了一个实际上具有全局作用域的函数模板,但尚未创建具体的函数或类型。
struct Base {
template <typename T>
static auto constexpr getFoo() {
return typename TChild::template B<T>{};
}
};
struct Child : Base<Child> {
template <typename T>
using B = T;
/* At this point we can instantiate Child::getFoo<int>()*/
}; /* Child is complete now */
此时 Child::getFoo<T>
的实例化是可能的,但它只需要 return 类型的函数。
using Bar = decltype(Child::getFoo<int>());
您可以在声明 B
之后将此声明放入 Child
,因为此时 B<int>
将完成。你仍然不能在 Base
案例二
您的解决方案声明了另一个模板 Foo,它没有在 Base
中实例化它。此模板未明确依赖于 TChild
,但需要 foo()
的原型在实例化时存在。
template <typename TChild>
struct Base {
template <typename T>
static auto foo(){
return typename TChild::template B<T>();
}
// Foo is a template
template <typename T>
using Foo = std::decay_t<decltype(foo<T>())>;
};
实例化发生在您将使用 Base::Foo<T>
的位置,而您实际上并没有。该声明在您的解决方案中是空操作。在 B
声明之后使用它是合法的。您不能在 Base
内或声明 B
之前的任何地方使用它。
现在,如果您 实际上 需要在 Base
中使用 B
的实例怎么办? trait class 解决方案来了:
案例三 特征可以是专门用于子 classes 或具体 classes 的模板,这是一种设计选择。特征作为 CRTP 基 class 的基 class 并且是一种混合形式。它的作用是为 CRTP 提供有用的声明。最灵活的特征命名可能的解决方案之一:
template <typename TChild, template<class> typename Trait>
struct Base : public Trait<TChild> {
// Trait<TChild>:: tells compiler that Foo is dependant on TChild
// and is declared in base class Trait. As compiler had reached this
// point, the substitution was successful and thus Trait is complete
using Foo = typename Trait<TChild>::template B<int>;
// Foo is assumed to be a complete type, we can use it here!
Foo make_foo() { return Foo{}; }
};
// Declaring trait template in this case.
template <typename T> struct ChildTrait;
// And specializing
template <>
struct ChildTrait<struct Child> {
template <typename T>
using B = T;
};
struct Child : Base<Child,ChildTrait> {
using Bar = typename Base::Foo;
};
static_assert(std::is_same<Child::B<int>,int>::value,"");
static_assert(std::is_same<Child::B<std::string>,std::string>::value,"");
这里的想法是 Trait<TChild> = ChildTrait<Child>
必须并且可以是 Base
中的完整 class,否则我们无法从中导出 Base
。稍作修改(省略 using
、static_assert
、typename
的使用)这将在 C++98 中编译,因为它不需要 decltype
。这种方法被一些标准组件的实现所使用,例如通过 std:: stream
s.
特征可以描述具体的存储类型、分配器等。重要的是生成的具体类型没有任何关系,但有一个在 Base
中声明的共享接口。