相关疑难解决方法(0)

我必须在何处以及为何要使用"模板"和"typename"关键字?

在模板,在那里,为什么我必须把typenametemplate上依赖的名字呢?究竟什么是依赖名称?我有以下代码:

template <typename T, typename Tail> // Tail will be a UnionNode too.
struct UnionNode : public Tail {
    // ...
    template<typename U> struct inUnion {
        // Q: where to add typename/template here?
        typedef Tail::inUnion<U> dummy; 
    };
    template< > struct inUnion<T> {
    };
};
template <typename T> // For the last node Tn.
struct UnionNode<T, void> {
    // ...
    template<typename U> struct inUnion {
        char fail[ -2 + (sizeof(U)%2) ]; // Cannot be instantiated for any …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++-faq dependent-name typename

1061
推荐指数
8
解决办法
15万
查看次数

什么时候需要"typename"关键字?

可能重复:
正式,什么是typename?
我必须在哪里以及为什么要放置模板和typename关键字?

考虑以下代码:

template<class K>
class C {
    struct P {};
    vector<P> vec;
    void f();
};

template<class K> void C<K>::f() {
    typename vector<P>::iterator p = vec.begin();
}
Run Code Online (Sandbox Code Playgroud)

为什么此示例中需要"typename"关键字?是否还有其他必须指定"typename"的情况?

c++ syntax templates typename

50
推荐指数
3
解决办法
3万
查看次数

标签 统计

c++ ×2

templates ×2

typename ×2

c++-faq ×1

dependent-name ×1

syntax ×1