std :: back_inserter需要const_reference在旧的GCC。为什么? [英] std::back_inserter needs const_reference on older GCC. Why?
问题描述
我目前正在查看一些可以在较新版本的GCC上编译的代码,但不是在较旧的版本上。在我的case我使用 std :: back_inserter
到 std :: copy
一些数据从一个数据结构到自定义数据结构。如果我忘记了 typedef value_type& const_reference
typedef在这个自定义数据结构中,但是,这不会在GCC 4.4上编译。相同的代码在GCC 4.5上编译和运行就好了。
I am currently looking at some code that can be compiled on newer versions of GCC but not on older ones. In my case I am using a std::back_inserter
to std::copy
some data from one data structure to a custom data structure. If I forget the typedef value_type & const_reference
typedef in this custom data structure however, this will not compile on GCC 4.4. The same code compiles and runs just fine on GCC 4.5.
这两个编译器版本之间有什么区别,使得代码在一个版本上编译,但不在其他。我猜想它与C ++ 11的实现有关,这在GCC 4.4中不太完整。可能是 decltype
或另一个新的C ++ 11关键字的东西,我猜猜。
What is the difference in between these two compiler versions, that makes the code compile on one version but not on the other. I would guess it has something to do with the implementation of C++11 which was much less complete in GCC 4.4. Probably something with decltype
or another new C++11 keyword, I would guess.
,如果我使用 std :: back_inserter
而不定义 const_reference
类型?我通常认为,必须实现完整的typedef( value_type
,引用
, const_reference
等),以便与STL算法库兼容?或者我可以安全地假设,如果我的代码编译在这种情况下,我不会调用任何危险的(例如移动语义,这将毁灭我的其他数据结构)。
Also is this code correct, if I use the std::back_inserter
without defining the const_reference
type? I usually thought that one has to implement the full set of typedefs (value_type
, reference
, const_reference
etc) in order to be compatible with the STL-algorithms library? Or can I safely assume that if my code compiles in this case I am not invoking anything dangerous (e.g. move semantics, which would destroy my other datastructure).
推荐答案
标准(1998)说 std :: back_insert_iterator
需要 Container :: const_reference
。在24.4.2.1模板类back_insert_iterator,[lib.back.insert.iterator]中,它表示:
The standard (1998) says that std::back_insert_iterator
needs Container::const_reference
. In "24.4.2.1 Template class back_insert_iterator", [lib.back.insert.iterator], it says:
back_insert_iterator<Container>&
operator=(typename Container::const_reference value);
2011标准只需要 Container :: value_type
,
The 2011 standard only wants Container::value_type
,
back_insert_iterator<Container>&
operator=(const typename Container::value_type& value);
back_insert_iterator<Container>&
operator=(typename Container::value_type&& value);
因此,为了兼容两个版本的C ++标准,定义 value_type
和 const_reference_type
。
So, to be compatible with both versions of the C++ standard, define both value_type
and const_reference_type
.
在GCC 4.4.6和4.5.1中, operator =
的定义是相同的( libstdc ++ - v3 / include / bits / stl_iterator.h
):
In both GCC 4.4.6 and 4.5.1, the definition of operator=
is identical (libstdc++-v3/include/bits/stl_iterator.h
):
back_insert_iterator&
operator=(typename _Container::const_reference __value)
{
container->push_back(__value);
return *this;
}
我得到两个编译器相同的错误,也许你需要请仔细检查是否使用正确的编译器版本。
and I get the same error with both compilers, perhaps you'll need to double check if you're using the correct compiler versions.
这篇关于std :: back_inserter需要const_reference在旧的GCC。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!