嵌套模板中的运算符= (T *r)

发布于 2024-08-09 18:36:59 字数 1853 浏览 3 评论 0原文

我有一个关于嵌套模板和赋值运算符重写的问题。 假设我想要一个引用计数类模板 _reference。这个_reference 现在简单 保存指向引用计数对象的指针。现在的问题是这一切都运行良好, 只要我用简单的类或结构来做到这一点。例如。 _reference ...,

但现在我想制作一个类模板,它是对转发其所持有的类的 std-vector 的引用。

不,我只是发布代码:(它现在不进行引用计数之类的事情,它只是提取我​​遇到的问题)

template <typename T>
class _reference
{
private:
    T* p_;

public:

// !!! this assignment seems only to work, when T is no class template already...
void operator= (T* r)                   
{
    p_ = r;
}

// WHILE this ALWAYS works as well...
void simplySetIt (T* r)                 
{
    p_ = r;
}
};

template <typename T>
class _ref_vector : public _reference<vector<T> >
{
};

void test2 ()
{
_reference<vector<long> > ref_ptr2;
_ref_vector<long>         ref_ptr3;

ref_ptr2 = new vector<long>;                    // works fine.

ref_ptr3 = new vector<long>;                // BUT: THIS doesnt work
    ref_ptr3.simplySetIt (new vector<long>);    // WHILE: this works fine...
}

MSVC-Error:

error C2679: binary '=' : no operator found which takes a right-hand operand of type 
'std::vector<_Ty> *' (or there is no acceptable conversion)

GCC-Error:

error: no match for 'operator=' in 'ptr3 = (((const stlp_std::allocator<long int>&)
((const stlp_std::allocator<long int>*)(& stlp_std::allocator<long int>()))), 
(((stlp_std::vector<long int, stlp_std::allocator<long int> >*)operator new(12u)), 
((<anonymous> != 0u) ? (<anonymous>->stlp_std::vector<_Tp, _Alloc>::vector [with 
_Tp = long int, _Alloc = stlp_std::allocator<long int>]
(<anonymous>), <anonymous>) : <anonymous>)))'

所以请有人能解释一下为什么赋值运算符在这里不起作用,而 simpleSetIt - 函数却起作用?

I have a problem concerning nested templates and the overriding of the assignment operator.
Say i want to have a refcounting class template _reference. This _reference for now simply
holds a pointer to the ref-counted object. The problem now is that this all works fine,
as long as im doing this with simple classes or structs. eg. _reference ...,

But now i want to make a class template that is a reference to a std-vector forwarding the class it holds.

Nah, i just post the code: (it doesnt do refcounting and that stuff right now, its just the extraction of the problem i have)

template <typename T>
class _reference
{
private:
    T* p_;

public:

// !!! this assignment seems only to work, when T is no class template already...
void operator= (T* r)                   
{
    p_ = r;
}

// WHILE this ALWAYS works as well...
void simplySetIt (T* r)                 
{
    p_ = r;
}
};

template <typename T>
class _ref_vector : public _reference<vector<T> >
{
};

void test2 ()
{
_reference<vector<long> > ref_ptr2;
_ref_vector<long>         ref_ptr3;

ref_ptr2 = new vector<long>;                    // works fine.

ref_ptr3 = new vector<long>;                // BUT: THIS doesnt work
    ref_ptr3.simplySetIt (new vector<long>);    // WHILE: this works fine...
}

MSVC-Error:

error C2679: binary '=' : no operator found which takes a right-hand operand of type 
'std::vector<_Ty> *' (or there is no acceptable conversion)

GCC-Error:

error: no match for 'operator=' in 'ptr3 = (((const stlp_std::allocator<long int>&)
((const stlp_std::allocator<long int>*)(& stlp_std::allocator<long int>()))), 
(((stlp_std::vector<long int, stlp_std::allocator<long int> >*)operator new(12u)), 
((<anonymous> != 0u) ? (<anonymous>->stlp_std::vector<_Tp, _Alloc>::vector [with 
_Tp = long int, _Alloc = stlp_std::allocator<long int>]
(<anonymous>), <anonymous>) : <anonymous>)))'

So please can anybody explain me why the assignment operator doesnt work here, while the simplySetIt - function does ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谎言月老 2024-08-16 18:36:59

基本运算符= 被隐式赋值运算符隐藏,因此它不再参与重载。您需要将 _ref_vector 编写为

template <typename T>
class _ref_vector : public _reference<vector<T> >
{
  using _reference<vector<T> >::operator=;
};

由于 simpleSetIt 没有编译器添加的版本,因此查找将在基类中找到它。

The base operator= gets hidden by implicit assignment operators, so that it doesn't take part in the overloading anymore. You need to write _ref_vector as

template <typename T>
class _ref_vector : public _reference<vector<T> >
{
  using _reference<vector<T> >::operator=;
};

As there is no compiler-added version of simplySetIt, lookup will find it in the base class.

素罗衫 2024-08-16 18:36:59

正如标准所说(13.5.3):

因为复制赋值运算符operator=是隐式声明的
如果用户未声明一个类 (12.8),则为基类赋值
运算符始终被复制赋值运算符隐藏
派生类。

As the standard says (13.5.3):

Because a copy assignment operator operator= is implicitly declared for
a class if not declared by the user (12.8), a base class assignment
operator is always hidden by the copy assignment operator of the
derived class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文