模板类构造函数不工作 [英] Template class constructor not working

查看:124
本文介绍了模板类构造函数不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我的其他问题到这里从std容器复制frm任意源对象我设法得到几乎在MSVC下工作的模板。不幸的是,编译器崩溃与添加一个构造函数接受所有种类的std容器的最新添加,我的真正的项目是在gcc反正。现在当我在gcc中使用这个模板时,我得到了几个错误,我不知道如何解决。

From my other question here Copying from std container frm arbitrary source object I managed to get the template almost working under MSVC. Unfortunately the compiler crashes with the newest addtion of adding a constructor to accept all kind of std containers, and my real project is in gcc anyway. Now when I use this template in gcc, I get several errors I don't know how to resolve.

template <class T> class ReadOnlyIterator
{
public:
    template <typename V, typename U>
    struct is_same
    {
        enum { value = 0 };
    };

    template <typename V>
    struct is_same<V, V>
    {
        enum { value = 1 };
    };

    template <bool, typename>
    struct enable_if
    {};

    template <typename V>
    struct enable_if<true, V>
    {
        typedef V type;
    };

template <typename Container>
    typename enable_if<
    is_same<T, typename Container::value_type>::value, ReadOnlyIterator<T>&>::type operator= (const Container &v)
    {
        return *this;
    }

    template <typename Container>
    ReadOnlyIterator(const Container &v, typename enable_if<is_same<T, typename Container::value_type>::value, void>::type * = 0)
    {
        mVector = v;
        mBegin = mVector.begin();
    }
};

我的目标是允许这样的分配:

My goal was to allow assignments like this:

std::vector<SimpleClass *>v;
std::list<SimpleClass *>l;
ReadOnlyIterator<SimpleClass *> t0 = v;
ReadOnlyIterator<SimpleClass *> &t1 = v;
ReadOnlyIterator<SimpleClass *> t2 = ReadOnlyIterator<SimpleClass *>(v);
ReadOnlyIterator<SimpleClass *> t3 = l;

t0 = v;
t0 = l;

我更新了上面的代码,并还原了我应用的错误更改。所以现在我只得到我尝试修复的原始问题:

I updated the code above and reverted the wrong changes I applied. So now I only get the original problem I tried to fix:

ReadOnlyIterator<SimpleClass *> &t1 = v;

可导致:

invalid initialization of reference of type 'ReadOnlyIterator<SimpleClass*>&' from expression of type 'std::vector<SimpleClass*, std::allocator<SimpleClass*> >'


推荐答案

在另一个模板类中编写模板类,必须给模板参数不同的名称:

As you already found out, if you write a template class within another template class, you have to give the template parameters different names:

template <typename U, typename V>
struct is_same<U, V>
{
    enum { value = 0 };
};

is_same 在指定专用类名时使用相同类型(您也可以将其命名为 U ,但在所有三个位置使用相同的名称:模板参数列表以及专门的类名):

In the specialization of is_same, you have to use the same types when specifying the specialized class name (you can also name it U, but use the same name on all three places: in the template parameter list as well as in the specialized class name):

template <typename V>
struct is_same<V, V>
{
    enum { value = 1 };
};

另外,如注释中所述,应该使这些帮助类 struct 而不是 class ;那么你不必写 public:

Also, as mentioned in the comments, you should make these helper-classes struct instead of class; then you don't have to write public:.

这篇关于模板类构造函数不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆