一千萬個為什麽

搜索

STL運算符=使用Visual Studio 2010更改行為?


我正在嘗試使用Visual Studio編譯QtScriptGenerator( gitorious ) 2010(C ++)並且遇到了編譯錯誤。在搜索解決方案時,由於VS2010實現STL和/或c ++ 0x一致性更改的變化,我偶爾會看到自VS2008以來引入的編譯破壞的引用。

任何想法下面發生了什麽,或者我如何解決它?如果違規代碼似乎是QtScriptGenerator的,我想我會更容易修復它..但在我看來,違規代碼可能在VS2010的STL實現中,我可能需要創建一個變通方法?

PS。我對模板和STL非常不熟悉。我有嵌入式和控制臺項目的背景,這些項目直到最近才被避免,以減少內存消耗和交叉編譯器風險。

編輯 - 似乎Visual Studio的 std :: copy 的實現發生了變化。

    C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(275) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'rpp::pp_output_iterator<_Container>' (or there is no acceptable conversion)
            with
            [
                _Container=std::string
            ]
            c:\qt\qtscriptgenerator\generator\parser\rpp\PP-iterator.h(75): could be 'rpp::pp_output_iterator<_Container> &rpp::pp_output_iterator<_Container>::operator =(const char &)'
            with
            [
                _Container=std::string
            ]
            while trying to match the argument list '(rpp::pp_output_iterator<_Container>, rpp::pp_output_iterator<_Container>)'
            with
            [
                _Container=std::string
            ]
            C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(2176) : see reference to function template instantiation '_Iter &std::_Rechecked<_OutIt,_OutIt>(_Iter &,_UIter)' being compiled
            with
            [
                _Iter=rpp::pp_output_iterator,
                _OutIt=rpp::pp_output_iterator,
                _UIter=rpp::pp_output_iterator
            ]
            c:\qt\qtscriptgenerator\generator\parser\rpp\PP-internal.h(83) : see reference to function template instantiation '_OutIt std::copy,_OutputIterator>(_InIt,_InIt,_OutIt)' being compiled
            with
            [
                _OutIt=rpp::pp_output_iterator,
                _Elem=char,
                _Traits=std::char_traits,
                _Alloc=std::allocator,
                _OutputIterator=rpp::pp_output_iterator,
                _InIt=std::_String_iterator,std::allocator>
            ]
            c:\qt\qtscriptgenerator\generator\parser\rpp\PP-發動機bits.h(500) : see reference to function template instantiation 'void rpp::_PP_internal::output_line<_OutputIterator>(const std::string &,int,_OutputIterator)' being compiled
            with
            [
                _OutputIterator=rpp::pp_output_iterator
            ]
    C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(275) : error C2582: 'operator =' function is unavailable in 'rpp::pp_output_iterator<_Container>'
            with
            [
                _Container=std::string
            ]

這是一些背景..

PP-internal.h

#ifndef PP_INTERNAL_H
#define PP_INTERNAL_H

#include 
#include 
namespace rpp {

namespace _PP_internal
{
..
64 template 
65 void output_line(const std::string &__filename, int __line, _OutputIterator __result)
66 {
67   std::string __msg;
68 
69   __msg += "# ";
70 
71   char __line_descr[16];
72   pp_snprintf (__line_descr, 16, "%d", __line);
73   __msg += __line_descr;
74 
75   __msg += " \"";
76 
77   if (__filename.empty ())
78     __msg += "";
79   else
80     __msg += __filename;
81 
82   __msg += "\"\n";
83   std::copy (__msg.begin (), __msg.end (), __result);
84 }

PP-發動機bits.h

#ifndef PP_ENGINE_BITS_H
#define PP_ENGINE_BITS_H

#include 

namespace rpp {

450 template 
451 void pp::operator() (_InputIterator __first, _InputIterator __last, _OutputIterator __result)
452 {
..
497           if (env.current_line != was)
498             {
499               env.current_line = was;
500               _PP_internal::output_line (env.current_file, env.current_line, __result);
501             }

.. and here's the definition of pp_output_iterator

PP-iterator.h

#ifndef PP_ITERATOR_H
#define PP_ITERATOR_H

#include 

namespace rpp {
..
template 
class pp_output_iterator
    : public std::iterator
{
    std::string &_M_result;

public:
    explicit pp_output_iterator(std::string &__result):
    _M_result (__result) {}

    inline pp_output_iterator &operator=(typename _Container::const_reference __v)
    {
    if (_M_result.capacity() == _M_result.size ())
        _M_result.reserve (_M_result.capacity() << 2);

    _M_result.push_back(__v);
    return *this;
    }

    inline pp_output_iterator &operator *() { return *this; }
    inline pp_output_iterator &operator ++() { return *this; }
    inline pp_output_iterator operator ++ (int) { return *this; }
};

最佳答案

I think that the problem is that std::copy is trying to use 'copy assignment' (operator=()) on your rpp::pp_output_iterator<> and there is no operator=() for that class template. I should say, there is an operator=() but it doesn't take the correct parameter to be the 'copy assignment' function (ie., it doesn't take a ``rpp::pp_output_iterator<>&parameter). I think that the existence of someoperator=()` function will prevent the compiler from generating a default (I don't have access to the standard document at the moment to verify this 100%).

Note that a type must be assignable (among other things, of course) to be considered an OutputIterator: http://www.sgi.com/tech/stl/OutputIterator.html

MSVC中以前版本的 std :: copy 可能沒有實際使用過賦值(只是因為OutputIterator必須支持它並不意味著 std :: copy 必須使用它),這就是為什麽它可能是VS2010中的“新”錯誤。 (由於訪問我的工具有限,我現在無法檢查)。

轉載註明原文: STL運算符=使用Visual Studio 2010更改行為?

猜你喜歡