如何为一个const模板参数定义一个复制构造函数? [英] How to define a copy constructor for a const template parameter?

查看:207
本文介绍了如何为一个const模板参数定义一个复制构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义迭代器,我无法满足创建一个 const 迭代器的场景,并使用非 const begin()。根据STL,这是合法的,可以用std :: string来演示:

I'm creating a custom iterator and I'm having trouble satisfying the scenario where I create a const iterator and initialize it with a non-const begin(). This is legal according to the STL and can be demonstrated with std::string:

#include <string>

using namespace std;

int main() {
    string::iterator a;
    string::const_iterator b = a;

    return 0;
}



我不知道如何使它工作:

I can't figure out how to make it work:

template<typename T>
class some_class {
};

int main() {
    some_class<int> a;

    // Works OK!
    const some_class<int> const_b = a;

    // error: conversion from 'some_class<int>' to non-scalar type 'const some_class<const int>'
    const some_class<const int> const_c = a;

    return 0;
}

UPDATE

@ALEXANDER KONSTANTINOV 提供了一个解决方案,但它不能满足所有可能的STL测试用例。我接下来要测试 @Bo Persson 的建议。将构造函数更改为 const some_class< U>&其他将允许它编译,但是迭代器a = const_iterator b 也会错误地成立。

@ALEXANDER KONSTANTINOV provided a solution but it does not satisfy all possible STL test cases. I'm testing @Bo Persson's suggestion next. Changing the constructor to const some_class<U>& other will allow it to compile but then iterator a = const_iterator b would also erroneously be true.

#include <string>

using namespace std;

template<typename T>
class some_class {
public:
    some_class() {
    }

    template<typename U>
    some_class(some_class<U>& other) {
    }
};

namespace name {
    typedef some_class<int> iterator;
    typedef const some_class<const int> const_iterator;
}

int main() {
    string::iterator a;
    string::const_iterator b = a;
    const string::iterator c;
    string::iterator d = c;
    string::const_iterator e = c;

    name::iterator f;
    name::const_iterator g = f;
    const name::iterator h;
    name::iterator i = h;
    name::const_iterator j = h; // <- Error

    return 0;
}

UPDATE

似乎有一些混淆关于添加 const 到构造函数。这里是一个测试用例:

There seems to be some confusion regarding adding const to the constructor. Here is a test case:

// This is not allowed by the STL
//string::const_iterator _a;
//string::iterator _b = _a; // <- Error!

// This should NOT compile!
name::const_iterator _a;
name::iterator _b = _a;


推荐答案

首先 - 你不能假设 std :: string :: const_iterator 只是常规迭代器的const version - 像这样 const std :: string :: iterator

First of all - you cannot assume that std::string::const_iterator is just "const version" of regular iterator - like this const std::string::iterator.

当你查看你的STL库实现(这只是来自gcc4.9.2 STL header for basic_string的例子):

When you look at your STL library implementation (this is just example from gcc4.9.2 STL header for basic_string):

  typedef __gnu_cxx::__normal_iterator<pointer, basic_string>  iterator;
  typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
                                                        const_iterator;

正如你所看到的 - 两个迭代器都有不同的是返回指针值 - 指针 vs const_pointer - 就是这样 - const iterator不是不能改变的东西,而是返回const指针/引用的东西你不能修改迭代器迭代的值。

As you can see - what differs both iterators is the return pointer value - pointer vs const_pointer - and that is the case - "const iterator" is not something that cannot change - but something that returns const pointer/references so you cannot modify the values the iterator iterates over.

所以 - 我们可以进一步调查,看看如何从非const到const版本的复制:

So - we can investigate further and see how the desired copying from non const to const version was achieved:

  // Allow iterator to const_iterator conversion
  template<typename _Iter>
    __normal_iterator(const __normal_iterator<_Iter,
          typename __enable_if<
           (std::__are_same<_Iter, typename _Container::pointer>::__value),
          _Container>::__type>& __i) _GLIBCXX_NOEXCEPT
    : _M_current(__i.base()) { }

所以,基本上 - 这个构造函数接受相同模板的任何实例( __ normal_iterator ) - 但它有 enable_if 只有const指针的实例。

So, basically - this constructor accepts any instance of the same template (__normal_iterator) - but it has enable_if closure to allow only instance of const pointer.

我相信你会在你的情况下做同样的事情

I believe you shall do the same in your case


  1. 有真正的const_iterator - 不只是常量迭代器的const版本

  2. 并且有来自const_iterator的模板构造函数和enable_if限制,以禁止从任何东西构造(我的意思是迭代器over ints从迭代器over std :: strings)






根据您的示例:


As per your example:

#include <type_traits>

template<typename T>
class some_class {
public:
    some_class() {
    }

    template <typename U>
    using allowed_conversion_from_non_const_version = std::enable_if_t<std::is_same<std::remove_cv_t<T>,U>::value>;

    template<typename U, typename EnableIf = allowed_conversion_from_non_const_version<U>>
    some_class(const some_class<U>&) {
    }

    template<typename U, typename EnableIf = allowed_conversion_from_non_const_version<U>>
    some_class& operator = (const some_class<U>&) {
    }
};

从这个例子中读取两件事:

Two things to read from this example:


  1. 还需要赋值运算符

  2. 只能从非const到const版本启用 - 这可以通过 enable_if / remove_cv remove_const 也可以 - 但为什么不构造volatile版本 - cv 短于 const

  1. Assignment operator is also needed
  2. You shall enable only from non-const to const version - and this is achieved by combination of enable_if/remove_cv (remove_const also works - but why not construct also volatile version - anyway cv is shorter than const)

这篇关于如何为一个const模板参数定义一个复制构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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