相关疑难解决方法(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;

我想创建一个模板作为folows.我想从矢量中删除项目列表vec1.并且我要删除的项目的索引存储在index_list.

#include <vector>

using namespace std;

template <typename a_type>
bool vector_remove(vector< a_type > & vec1, vector< int > index_list)
{
    //index_list is sorted in order from small to large.

    if(index_list.size() > vec1.size())
    {
        cout << "ERROR in 'vector_remove()': index_list is longer than vec1."<<endl;
        return false;
    }
    if(index_list.size() == vec1.size())
    {
        vec1.clear();
        return true;
    }
    vector< int >::iterator ind_pt = index_list.begin();
    vector< a_type >::iterator vec1_pre = vec1.begin();
    vector< a_type >::iterator vec1_pos = vec1.begin();
    int vec1_ind = 0; …
Run Code Online (Sandbox Code Playgroud)

c++ templates vector

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

标签 统计

c++ ×2

templates ×2

c++-faq ×1

dependent-name ×1

typename ×1

vector ×1