Tom*_*uwé 6 c++ templates metaprogramming crtp
当试着用GCC 4.6.0编译这个(类似CRTP的)代码时:
template<template<class> class T> struct A;
template<class T>
struct B: A<B<T>::template X> {
template <class U> struct X { U mem; };
};
B<int> a;Run Code Online (Sandbox Code Playgroud)
我得到错误消息"test.cpp:3:26:错误:'struct B <int>'中没有名为'X'的类模板".为什么X似乎在类定义之外是不可见的?
正如 Emile Cormier在这里正确指出的那样,问题在于A,的实例化位置B仍然是不完整的类型,并且您无法使用内部模板。
解决方案是将模板移到X模板之外B。如果它独立于 T模板的特定实例化B,只需将其移动到命名空间级别,如果它依赖于实例化,则可以使用类型特征:
template <typename T>
struct inner_template
{
template <typename U> class tmpl { U mem; }; // can specialize for particular T's
};
template <typename T>
struct B : A< inner_template<T>::template tmpl >
{
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2798 次 |
| 最近记录: |