3

我真的不明白发生了什么。我正在使用 MSVC 2012,Boost 51。这段代码应该可以正常工作,但它没有被编译:

#include "stdafx.h"
#include <string>
#include <boost/variant/recursive_variant.hpp>

namespace NsSemSDK
{

struct STreeConstructionRuleRegexp {
    std::string m_strEntity;
};

struct STreeConstructionRuleString {
    std::string m_strEntity;
};

struct STreeConstructionRuleIdentifier {
    std::string m_strEntity;
};

typedef int STreeConstructionRuleNumber;

typedef boost::variant<STreeConstructionRuleRegexp, STreeConstructionRuleNumber> STreeConstructionRuleOperand; // STreeConstructionRuleString, STreeConstructionRuleIdentifier

}

std::ostream& operator<<(std::ostream& stream, const NsSemSDK::STreeConstructionRuleRegexp& val)
{
    return stream << '\'' << val.m_strEntity << '\'';
}

std::ostream& operator<<(std::ostream& stream, const NsSemSDK::STreeConstructionRuleString& val)
{
    return stream << '"' << val.m_strEntity << '"';
}

std::ostream& operator<<(std::ostream& stream, const NsSemSDK::STreeConstructionRuleIdentifier& val)
{
    return stream << val.m_strEntity;
}

using namespace NsSemSDK;

int _tmain(int argc, _TCHAR* argv[])
{
    STreeConstructionRuleNumber num = 1024;
    STreeConstructionRuleOperand operand = num;
    std::cout << operand << std::endl;
}

我得到的是:

1>------ Build started: Project: ConsoleApplication3, Configuration: Debug Win32 ------
1>  Source.cpp
1>c:\users\vsevolod.sauta\documents\visual studio 2012\projects\consoleapplication3\consoleapplication3\boost\variant\detail\variant_io.hpp(64): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const T0' (or there is no acceptable conversion)
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\ostream(695): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\ostream(742): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\ostream(780): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\ostream(827): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\ostream(953): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const signed char *)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\ostream(960): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,signed char)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\ostream(967): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const unsigned char *)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\ostream(974): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,unsigned char)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\ostream(984): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>,T>(std::basic_ostream<_Elem,_Traits> &&,const _Ty &)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              T=T0,
1>              _Ty=T0
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\ostream(1101): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const std::error_code &)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\users\vsevolod.sauta\documents\visual studio 2012\projects\consoleapplication3\consoleapplication3\boost\blank.hpp(93): or       'std::basic_ostream<_Elem,_Traits> &boost::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const boost::blank &)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\ostream(201): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ostream<_Elem,_Traits> &(__cdecl *)(std::basic_ostream<_Elem,_Traits> &))'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\ostream(207): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ios<_Elem,_Traits> &(__cdecl *)(std::basic_ios<_Elem,_Traits> &))'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\ostream(214): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::ios_base &(__cdecl *)(std::ios_base &))'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\ostream(221): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::_Bool)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\ostream(241): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(short)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\ostream(275): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned short)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\ostream(295): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(int)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\ostream(320): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned int)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\ostream(340): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\ostream(360): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned long)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\ostream(381): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__int64)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\ostream(401): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned __int64)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\ostream(422): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(float)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\ostream(442): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(double)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\ostream(462): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long double)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\ostream(482): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(const void *)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\ostream(502): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_streambuf<_Elem,_Traits> *)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          while trying to match the argument list '(std::basic_ostream<_Elem,_Traits>, const T0)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\users\vsevolod.sauta\documents\visual studio 2012\projects\consoleapplication3\consoleapplication3\boost\variant\variant.hpp(859) : see reference to function template instantiation 'void boost::detail::variant::printer<OStream>::operator ()<const T>(const T &) const' being compiled
1>          with
1>          [
1>              OStream=std::basic_ostream<char,std::char_traits<char>>,
1>              T=T0
1>          ]
1>          c:\users\vsevolod.sauta\documents\visual studio 2012\projects\consoleapplication3\consoleapplication3\boost\variant\variant.hpp(859) : see reference to function template instantiation 'void boost::detail::variant::printer<OStream>::operator ()<const T>(const T &) const' being compiled
1>          with
1>          [
1>              OStream=std::basic_ostream<char,std::char_traits<char>>,
1>              T=T0
1>          ]
1>          c:\users\vsevolod.sauta\documents\visual studio 2012\projects\consoleapplication3\consoleapplication3\boost\variant\detail\visitation_impl.hpp(130) : see reference to function template instantiation 'void boost::detail::variant::invoke_visitor<Visitor>::internal_visit<const T>(T &,int)' being compiled
1>          with
1>          [
1>              Visitor=boost::detail::variant::printer<std::basic_ostream<char,std::char_traits<char>>>,
1>              T=T0
1>          ]
1>          c:\users\vsevolod.sauta\documents\visual studio 2012\projects\consoleapplication3\consoleapplication3\boost\variant\detail\visitation_impl.hpp(130) : see reference to function template instantiation 'void boost::detail::variant::invoke_visitor<Visitor>::internal_visit<const T>(T &,int)' being compiled
1>          with
1>          [
1>              Visitor=boost::detail::variant::printer<std::basic_ostream<char,std::char_traits<char>>>,
1>              T=T0
1>          ]
1>          c:\users\vsevolod.sauta\documents\visual studio 2012\projects\consoleapplication3\consoleapplication3\boost\variant\detail\visitation_impl.hpp(173) : see reference to function template instantiation 'void boost::detail::variant::visitation_impl_invoke_impl<Visitor,VoidPtrCV,T>(int,Visitor &,VoidPtrCV,T *,boost::mpl::true_)' being compiled
1>          with
1>          [
1>              Visitor=boost::detail::variant::invoke_visitor<boost::detail::variant::printer<std::basic_ostream<char,std::char_traits<char>>>>,
1>              VoidPtrCV=const void *,
1>              T=T0
1>          ]
1>          c:\users\vsevolod.sauta\documents\visual studio 2012\projects\consoleapplication3\consoleapplication3\boost\variant\detail\visitation_impl.hpp(256) : see reference to function template instantiation 'void boost::detail::variant::visitation_impl_invoke<Visitor,VoidPtrCV,T0,NoBackupFlag>(int,Visitor &,VoidPtrCV,T *,NoBackupFlag,int)' being compiled
1>          with
1>          [
1>              Visitor=boost::detail::variant::invoke_visitor<boost::detail::variant::printer<std::basic_ostream<char,std::char_traits<char>>>>,
1>              VoidPtrCV=const void *,
1>              NoBackupFlag=boost::variant<NsSemSDK::STreeConstructionRuleRegexp,NsSemSDK::STreeConstructionRuleNumber>::has_fallback_type_,
1>              T=T0
1>          ]
1>          c:\users\vsevolod.sauta\documents\visual studio 2012\projects\consoleapplication3\consoleapplication3\boost\variant\variant.hpp(1803) : see reference to function template instantiation 'void boost::detail::variant::visitation_impl<first_which,first_step,Visitor,VoidPtrCV,boost::variant<T0_,T1>::has_fallback_type_>(const int,const int,Visitor &,VoidPtrCV,boost::mpl::false_,NoBackupFlag,Which *,step0 *)' being compiled
1>          with
1>          [
1>              Visitor=boost::detail::variant::invoke_visitor<boost::detail::variant::printer<std::basic_ostream<char,std::char_traits<char>>>>,
1>              VoidPtrCV=const void *,
1>              T0_=NsSemSDK::STreeConstructionRuleRegexp,
1>              T1=NsSemSDK::STreeConstructionRuleNumber,
1>              NoBackupFlag=boost::variant<NsSemSDK::STreeConstructionRuleRegexp,NsSemSDK::STreeConstructionRuleNumber>::has_fallback_type_,
1>              Which=first_which,
1>              step0=first_step
1>          ]
1>          c:\users\vsevolod.sauta\documents\visual studio 2012\projects\consoleapplication3\consoleapplication3\boost\variant\variant.hpp(1825) : see reference to function template instantiation 'void boost::variant<T0_,T1>::internal_apply_visitor_impl<Visitor,const void*>(int,int,Visitor &,VoidPtrCV)' being compiled
1>          with
1>          [
1>              T0_=NsSemSDK::STreeConstructionRuleRegexp,
1>              T1=NsSemSDK::STreeConstructionRuleNumber,
1>              Visitor=boost::detail::variant::invoke_visitor<boost::detail::variant::printer<std::basic_ostream<char,std::char_traits<char>>>>,
1>              VoidPtrCV=const void *
1>          ]
1>          c:\users\vsevolod.sauta\documents\visual studio 2012\projects\consoleapplication3\consoleapplication3\boost\variant\variant.hpp(1825) : see reference to function template instantiation 'void boost::variant<T0_,T1>::internal_apply_visitor_impl<Visitor,const void*>(int,int,Visitor &,VoidPtrCV)' being compiled
1>          with
1>          [
1>              T0_=NsSemSDK::STreeConstructionRuleRegexp,
1>              T1=NsSemSDK::STreeConstructionRuleNumber,
1>              Visitor=boost::detail::variant::invoke_visitor<boost::detail::variant::printer<std::basic_ostream<char,std::char_traits<char>>>>,
1>              VoidPtrCV=const void *
1>          ]
1>          c:\users\vsevolod.sauta\documents\visual studio 2012\projects\consoleapplication3\consoleapplication3\boost\variant\variant.hpp(1847) : see reference to function template instantiation 'void boost::variant<T0_,T1>::internal_apply_visitor<boost::detail::variant::invoke_visitor<Visitor>>(boost::detail::variant::invoke_visitor<Visitor> &) const' being compiled
1>          with
1>          [
1>              T0_=NsSemSDK::STreeConstructionRuleRegexp,
1>              T1=NsSemSDK::STreeConstructionRuleNumber,
1>              Visitor=boost::detail::variant::printer<std::basic_ostream<char,std::char_traits<char>>>
1>          ]
1>          c:\users\vsevolod.sauta\documents\visual studio 2012\projects\consoleapplication3\consoleapplication3\boost\variant\variant.hpp(1847) : see reference to function template instantiation 'void boost::variant<T0_,T1>::internal_apply_visitor<boost::detail::variant::invoke_visitor<Visitor>>(boost::detail::variant::invoke_visitor<Visitor> &) const' being compiled
1>          with
1>          [
1>              T0_=NsSemSDK::STreeConstructionRuleRegexp,
1>              T1=NsSemSDK::STreeConstructionRuleNumber,
1>              Visitor=boost::detail::variant::printer<std::basic_ostream<char,std::char_traits<char>>>
1>          ]
1>          c:\users\vsevolod.sauta\documents\visual studio 2012\projects\consoleapplication3\consoleapplication3\boost\variant\detail\variant_io.hpp(88) : see reference to function template instantiation 'void boost::variant<T0_,T1>::apply_visitor<boost::detail::variant::printer<OStream>>(Visitor &) const' being compiled
1>          with
1>          [
1>              T0_=NsSemSDK::STreeConstructionRuleRegexp,
1>              T1=NsSemSDK::STreeConstructionRuleNumber,
1>              OStream=std::basic_ostream<char,std::char_traits<char>>,
1>              Visitor=boost::detail::variant::printer<std::basic_ostream<char,std::char_traits<char>>>
1>          ]
1>          c:\users\vsevolod.sauta\documents\visual studio 2012\projects\consoleapplication3\consoleapplication3\boost\variant\detail\variant_io.hpp(88) : see reference to function template instantiation 'void boost::variant<T0_,T1>::apply_visitor<boost::detail::variant::printer<OStream>>(Visitor &) const' being compiled
1>          with
1>          [
1>              T0_=NsSemSDK::STreeConstructionRuleRegexp,
1>              T1=NsSemSDK::STreeConstructionRuleNumber,
1>              OStream=std::basic_ostream<char,std::char_traits<char>>,
1>              Visitor=boost::detail::variant::printer<std::basic_ostream<char,std::char_traits<char>>>
1>          ]
1>          c:\users\vsevolod.sauta\documents\visual studio 2012\projects\consoleapplication3\consoleapplication3\source.cpp(47) : see reference to function template instantiation 'std::basic_ostream<_Elem,_Traits> &boost::operator <<<char,std::char_traits<char>,NsSemSDK::STreeConstructionRuleRegexp,NsSemSDK::STreeConstructionRuleNumber,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_>(std::basic_ostream<_Elem,_Traits> &,const boost::variant<T0_,T1> &)' being compiled
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              T0_=NsSemSDK::STreeConstructionRuleRegexp,
1>              T1=NsSemSDK::STreeConstructionRuleNumber
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我不知道什么是 T0 类型以及为什么将其转换为 T0。我希望它是 boost::variant 内部的东西。但是我仍然没有任何线索,为什么我会到达那里:定义了变体中所有类型的运算符“<<”。

4

1 回答 1

4

您应该在命名空间 NsSemSDK 内部定义,以便ADLoperator<<可以找到这些函数。(示例。)

#include <string>
#include <iostream>
#include <boost/variant.hpp>

namespace NsSemSDK
{

struct STreeConstructionRuleRegexp {
    std::string m_strEntity;
};

struct STreeConstructionRuleString {
    std::string m_strEntity;
};

struct STreeConstructionRuleIdentifier {
    std::string m_strEntity;
};

typedef int STreeConstructionRuleNumber;

std::ostream& operator<<(std::ostream& stream,
                         const STreeConstructionRuleRegexp& val)
{
    return stream << '\'' << val.m_strEntity << '\'';
}

std::ostream& operator<<(std::ostream& stream,
                         const STreeConstructionRuleString& val)
{
    return stream << '"' << val.m_strEntity << '"';
}

std::ostream& operator<<(std::ostream& stream,
                         const STreeConstructionRuleIdentifier& val)
{
    return stream << val.m_strEntity;
}

typedef boost::variant<
    STreeConstructionRuleRegexp,
    STreeConstructionRuleNumber
> STreeConstructionRuleOperand;

// STreeConstructionRuleString, STreeConstructionRuleIdentifier
}

using namespace NsSemSDK;

int main()
{
    STreeConstructionRuleNumber num = 1024;
    STreeConstructionRuleOperand operand = num;
    std::cout << operand << std::endl;
}

输出

1024

于 2012-10-18T12:48:07.693 回答