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

有时我看到gcc在使用模板时吐出的一些非常难以理解的错误消息......具体来说,我遇到了一些问题,看似正确的声明引起了非常奇怪的编译错误,通过在"typename"关键字前加上前缀而神奇地消失了声明的开头...(例如,就在上周,我宣布两个迭代器作为另一个模板化类的成员,我必须这样做)...

关于typename的故事是什么?

c++ templates

116
推荐指数
6
解决办法
5万
查看次数

依赖范围; 需要在前面输入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万
查看次数

SFINAE基于班级成员存在/缺席

我对SFINAE有基本的了解,比如说enable_if有效.我最近遇到了这个答案,我花了一个多小时试图了解它实际上是如何工作无济于事的.

此代码的目标是根据类中是否包含特定成员来重载函数.这是复制的代码,它使用C++ 11:

template <typename T> struct Model
{
    vector<T> vertices;

    void transform( Matrix m )
    {
        for(auto &&vertex : vertices)
        {
          vertex.pos = m * vertex.pos;
          modifyNormal(vertex, m, special_());
        }
    }

private:

    struct general_ {};
    struct special_ : general_ {};
    template<typename> struct int_ { typedef int type; };

    template<typename Lhs, typename Rhs,
             typename int_<decltype(Lhs::normal)>::type = 0>
    void modifyNormal(Lhs &&lhs, Rhs &&rhs, special_) {
       lhs.normal = rhs * lhs.normal;
    }

    template<typename Lhs, typename Rhs>
    void modifyNormal(Lhs …
Run Code Online (Sandbox Code Playgroud)

c++ sfinae c++11

6
推荐指数
1
解决办法
323
查看次数

继承typedef?

我最近被一些代码示例搞糊涂了 - 有时看起来继承基类暴露的typedef有效,有时它似乎没有.

我的问题是

  • 它为什么不总是有效?
  • 它/将不会起作用的情况是什么?
  • 什么是不起作用的好的解决方法?

这是一些特定的代码:

// First example: Inheriting `static const int ...`
// Basic TypeList object
template<typename... Ts>
struct TypeList {
    static const int size = sizeof...(Ts);
};

// Repeat metafunction
template<typename T>
struct repeat;

template<typename... Ts>
struct repeat<TypeList<Ts...>> : TypeList<Ts..., Ts...> {};

// Checks
typedef TypeList<int, float, char> MyList;

static_assert(MyList::size == 3, "D:");
static_assert(repeat<MyList>::size == 6, "D:");


// Second example: Inheriting typedefs
// Meta function to compute a bundle of types
template <typename T>
struct …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance templates template-meta-programming c++11

2
推荐指数
1
解决办法
2184
查看次数

使用带有奇怪重复模板模式的关键字时出错

有谁知道为什么下面的代码编译找不到N::balance_type

我正在使用 Stroustrup C++ 4th Ed Page 771 中的这个例子。它是奇怪的重复模板模式

class Bal {};

template<typename N>
struct Node_base : N::balance_type {
};

template<typename Val, typename Balance>
struct Search_node : public Node_base<Search_node<Val,Balance>> {
    using balance_type = Balance;
};

int main()
{
    Search_node<int, Bal> sn;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

汇编:

clang++ -std=c++11 -Wall -pedantic test197.cc && ./a.out
test197.cc:4:23: error: no type named 'balance_type' in 'Search_node<int, Bal>'
struct Node_base : N::balance_type {
                   ~~~^~~~~~~~~~~~
test197.cc:8:29: note: in instantiation of template class
      'Node_base<Search_node<int, Bal> …
Run Code Online (Sandbox Code Playgroud)

c++

2
推荐指数
1
解决办法
55
查看次数

下面使用“typename”有什么区别?

typename在函数的返回类型之前使用“”与在函数声明中不使用它(如下所示)有什么区别?

如果我们根本不使用它,会有什么不同呢?

template< class T > typename std::remove_reference<T>::type&& move( T&& t );
template< class T > std::remove_reference_t<T>&& move( T&& t ) ;
Run Code Online (Sandbox Code Playgroud)

c++ templates return-type typename function-templates

2
推荐指数
1
解决办法
118
查看次数

基于std :: is_convertible的std :: enable_if无法正确推断模板

我有以下代码:

#include <iostream>
#include <type_traits>

template <typename T, typename std::enable_if
                                 <std::is_convertible<int, T>::value, T>::type>
void func(T a)
{
    std::cout << a << std::endl;
}

template <typename T, typename std::enable_if
                                 <!std::is_convertible<int, T>::value, T>::type>
void func(T a)
{
    a.print();
}

class Test
{
public:
    void print()
    {
        std::cout << "Test" << std::endl;
    }
};    

int main()
{
    func(3);
    func("Test");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

使用此代码,我希望第一个调用func打印输出3int实际上可以转换为int,应该调用第一个专业化名称),第二个调用func打印输出TestTest()不能转换为int,所以应该调用第二个专业化名称)。但是,我得到了一个编译器错误:

prog.cpp:在函数'int main()'中:

prog.cpp:27:8:错误:没有匹配的函数调用'func(int)'

prog.cpp:5:6:注意:候选:模板[类T,类型名称std …

c++ enable-if

1
推荐指数
1
解决办法
1087
查看次数