使用g ++时意外的编译问题-std = c ++ 0x [英] Unexpected compilation problem with g++ -std=c++0x

查看:190
本文介绍了使用g ++时意外的编译问题-std = c ++ 0x的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用g ++ -std = c ++ 0x编译时,我有一些编译问题将类型T的元素推回到向量。



这是一个最小的例子:

  #include< vector> 

using namespace std;

class A {
public:
A(){}

A& operator =(A& orig){
return * this;
}
};

int main(int argc,char ** argv){
A a;
vector< A> b;
A c = a; //这很好
b.push_back(a); //这不是,但是只有当用-std = c ++ 0x编译时!
return 0;
}

它使用g ++ -Wall -pedantic编译得很好,使用g ++编译-Wall -pedantic -std = c ++ 0x:

 在/ usr / include / 4.4 / vector:69,
from min.cpp:1:
/usr/include/c++/4.4/bits/vector.tcc:在成员函数'void std :: vector< _Tp,_Alloc> :: _ M_insert_aux(__ gnu_cxx :: __ normal_iterator< typename std :: _ Vector_base< _Tp,_Alloc> :: _ Tp_alloc_type :: pointer,std :: vector< _Tp,_Alloc>,_Args& const A& _Tp = A,_Alloc = std :: allocator< A>]':
/usr/include/c++/4.4/bits/stl_vector.h:741: _Tp,_Alloc> :: push_back(const _Tp&)[with _Tp = A,_Alloc = std :: allocator< A>]
min.cpp:20:从这里实例化
/ usr / include /c++/4.4/bits/vector.tcc:314:错误:在'__position .__中没有匹配'operator ='gnu_cxx :: __ normal_iterator <_Iterator,_Container> :: operator * [with _Iterator = A *,_Container = std: :vector< A,std :: allocator< A> >]()=((const A&)((const A *)std :: forward [with _Tp = const A& )'
min.cpp:11:note:candidate are:A& A :: operator =(A&)
在从/usr/include/c++/4.4/vector:61,
从min.cpp:1:
/ usr / include / C ++ / 4.4 / bits / stl_algobase.h:在静态成员函数'static _BI2 std :: __ copy_move_backward< true,false,std :: random_access_iterator_tag> :: __ copy_move_b(_BI1,_BI1,_BI2)[with _BI1 = A *,_BI2 = A *]':
/usr/include/c++/4.4/bits/stl_algobase.h:595:从'_BI2 std :: __ copy_move_backward_a(_BI1,_BI1,_BI2)实例化[with bool _IsMove = true,_BI1 = A *,_BI2 = A *]'
/usr/include/c++/4.4/bits/stl_algobase.h:605:实例化为'_BI2 std :: __ copy_move_backward_a2(_BI1,_BI1,_BI2)[with bool _IsMove = true,_BI1 = A *,_BI2 = A *]'
/usr/include/c++/4.4/bits/stl_algobase.h:676:从'_BI2 std :: move_backward(_BI1,_BI1,_BI2)与_BI1 = A *,_BI2 = A *]'
/usr/include/c++/4.4/bits/vector.tcc:308:实例化为'void std :: vector< _Tp,_Alloc> :: _ M_insert_aux __gnu_cxx :: __ normal_iterator< typename std :: _ Vector_base< _Tp,_Alloc> :: _ Tp_alloc_type :: pointer,std :: vector< _Tp,_Alloc> >,_Args&& ...)[with _Args = const A& _Tp = A,_Alloc = std :: allocator< A>]'
/usr/include/c++/4.4/bits/stl_vector.h:741: 'void std :: vector< _Tp,_Alloc> :: push_back(const _Tp&)[with _Tp = A,_Alloc = std :: allocator ]
min.cpp:20: b $ b /usr/include/c++/4.4/bits/stl_algobase.h:561:error:没有匹配'operator ='in'* - __result = std :: move [with _Tp = A&](( A&)( - __last)))'
min.cpp:11:note:candidate are:A& A :: operator =(A&)

所以看来它找不到正确的操作符= of A.为什么?为什么当我传递A时,用_Iterator = A * 来表示

标准容器元素上的语言标准引发的可分配要求要求 t = u 表达式有效,即使 u 是一个const对象。由于C ++ 98(见23.1 / 4)



你违反了这个要求,因为你的赋值操作符不接受const对象。这意味着您的类 A 不能用作容器元素类型。



为什么它在C + +03是不相关的。它意外工作。从错误消息可以看出,库的C ++ 0x实现使用一些C ++ 0x的特定功能(如 std :: move ),这是什么使得上述要求发挥作用。但无论如何,C ++ 03实现(甚至C ++ 98实现)也可能无法为您的 A 编译。



使用 A c = a; 的例子是不相关的,因为它根本不使用赋值运算符。 >

为了修正错误,您应该接受const引用或值接受参数。


I have some compilation problems pushing back elements of type T to a vector when compiling with g++ -std=c++0x.

This is a minimal example:

#include <vector>

using namespace std;

class A {
public:
    A() { }

    A& operator=(A &orig) {
        return *this;
    }
};

int main(int argc, char **argv) {
    A a;
    vector<A> b;
    A c = a; // This is fine
    b.push_back(a); // This is not, but only when compiling with -std=c++0x!
    return 0;
}

It compiles fine with g++ -Wall -pedantic, but it gives this error when compiling with g++ -Wall -pedantic -std=c++0x:

 In file included from /usr/include/c++/4.4/vector:69,
                 from min.cpp:1:
/usr/include/c++/4.4/bits/vector.tcc: In member function ‘void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, _Args&& ...) [with _Args = const A&, _Tp = A, _Alloc = std::allocator<A>]’:
/usr/include/c++/4.4/bits/stl_vector.h:741:   instantiated from ‘void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = A, _Alloc = std::allocator<A>]’
min.cpp:20:   instantiated from here
/usr/include/c++/4.4/bits/vector.tcc:314: error: no match for ‘operator=’ in ‘__position.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = A*, _Container = std::vector<A, std::allocator<A> >]() = ((const A&)((const A*)std::forward [with _Tp = const A&](((const A&)((const A*)__args#0)))))’
min.cpp:11: note: candidates are: A& A::operator=(A&)
In file included from /usr/include/c++/4.4/vector:61,
                 from min.cpp:1:
/usr/include/c++/4.4/bits/stl_algobase.h: In static member function ‘static _BI2 std::__copy_move_backward<true, false, std::random_access_iterator_tag>::__copy_move_b(_BI1, _BI1, _BI2) [with _BI1 = A*, _BI2 = A*]’:
/usr/include/c++/4.4/bits/stl_algobase.h:595:   instantiated from ‘_BI2 std::__copy_move_backward_a(_BI1, _BI1, _BI2) [with bool _IsMove = true, _BI1 = A*, _BI2 = A*]’
/usr/include/c++/4.4/bits/stl_algobase.h:605:   instantiated from ‘_BI2 std::__copy_move_backward_a2(_BI1, _BI1, _BI2) [with bool _IsMove = true, _BI1 = A*, _BI2 = A*]’
/usr/include/c++/4.4/bits/stl_algobase.h:676:   instantiated from ‘_BI2 std::move_backward(_BI1, _BI1, _BI2) [with _BI1 = A*, _BI2 = A*]’
/usr/include/c++/4.4/bits/vector.tcc:308:   instantiated from ‘void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, _Args&& ...) [with _Args = const A&, _Tp = A, _Alloc = std::allocator<A>]’
/usr/include/c++/4.4/bits/stl_vector.h:741:   instantiated from ‘void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = A, _Alloc = std::allocator<A>]’
min.cpp:20:   instantiated from here
/usr/include/c++/4.4/bits/stl_algobase.h:561: error: no match for ‘operator=’ in ‘* -- __result = std::move [with _Tp = A&](((A&)(-- __last)))’
min.cpp:11: note: candidates are: A& A::operator=(A&)

So it seems that it doesn't find the right operator= of A. Why? Why it states with _Iterator = A* when I'm passing A?

解决方案

The Assignable requirement imposted by the language standard on the standard container elements requires the t = u expression to be valid even if u is a const object. The requirement was defined that way since C++98 (see 23.1/4)

You violated that requirement, since your assignment operator does not accept const objects. This immediately mean that your class A cannot be used as a container element type.

Why it worked in C++03 is rather irrelevant. It worked by accident. It is obvious from the error message that the C++0x implementation of the library uses some C++0x specific features (like std::move), which is what makes the above requirement to come into play. But anyway, a C++03 implementation (and even C++98 implementation) can also fail to compile for your A.

Your example with A c = a; is irrelevant, since it does not use the assignment operator at all (why is it here?).

In order to fix the error you should either accept the parameter by const reference or by value.

这篇关于使用g ++时意外的编译问题-std = c ++ 0x的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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