C++ 代码用g+编译+;但VC失败了++;10

C++ 代码用g+编译+;但VC失败了++;10,c++,visual-c++,g++,C++,Visual C++,G++,我正在将我的一些Linux程序移植到Windows。我想确保我的代码用MinGW和Visual C++ 10编译,而没有太多代码< >定义< /C> >和除了这个问题外,我已经解决了大部分问题。我有一个类似于下面的代码,它用G+4.4.3编译,但不是用Visual C++ 10:< /P> #include <iostream> #include <vector> #include <array> #include <algorithm> #inc

我正在将我的一些Linux程序移植到Windows。我想确保我的代码用MinGW和Visual C++ 10编译,而没有太多代码< >定义< /C> >和<代码>除了这个问题外,我已经解决了大部分问题。我有一个类似于下面的代码,它用G+4.4.3编译,但不是用Visual C++ 10:< /P>
#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
#include <functional>
#include <cmath>
#include <iterator>

typedef unsigned char UINT8;
const UINT8 MAX_COLL = 10;

template< typename BaseT, typename ExpT >
struct fopow {
    BaseT operator() ( BaseT base, ExpT exp ) const {
        return std::pow( base, exp );
    }
};

int main() {
    using namespace std;
    using namespace std::placeholders;

    array<int, MAX_COLL> intCollection_init = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    vector<int> intCollection( intCollection_init.begin(), intCollection_init.end() );

    transform( intCollection.begin(), intCollection.end(),
               ostream_iterator<int>( cout, " " ),
               bind( fopow<int, int>(), 3, _1 )
            );
    cout << endl;

    transform( intCollection.begin(), intCollection.end(),
               ostream_iterator<int>( cout, " " ),
               bind( fopow<int, int>(), _1, 3 )
            );
    cout << endl;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
typedef无符号字符UINT8;
const UINT8 MAX_COLL=10;
模板
结构fopow{
BaseT运算符()(BaseT base,ExpT exp)常量{
返回标准::功率(基准,经验);
}
};
int main(){
使用名称空间std;
使用名称空间std::占位符;
数组intCollection_init={1,2,3,4,5,6,7,8,9,10};
向量intCollection(intCollection_init.begin(),intCollection_init.end());
转换(intCollection.begin(),intCollection.end(),
ostream_迭代器(cout,“”),
绑定(fopow(),3,_1)
);

cout将
typedef BaseT result\u type;
添加到
fopow
的定义中。在
decltype
之前需要的模板黑客正在试图弄清楚
fopow::operator()
的返回类型。(是的,你是对的:它在
bind()中丢失了)
call;特别是第一个。)


或者,如果您愿意,您可以使用@ecatmur:derivate from
std::binary\u function
。这也会将
结果类型
添加到模板中,以及
第一个参数类型
第二个参数类型
绑定
不需要参数类型。

添加
typedef-BaseT结果类型
fopow
的定义。在
decltype
之前几天需要的模板黑客正在试图弄清楚
fopow::operator()
的返回类型是什么。(是的,你是对的:它在
bind()
调用中丢失了;特别是第一个。)


或者,如果您愿意,您可以使用@ecatmur:derivate from
std::binary\u function
建议的修复方法。这也会将
result\u type
添加到模板中,以及
first\u-argument\u type
second\u-argument\u-type
不需要参数类型。

std::bind
假设它在VS2010中:它实际上是
std::tr1::bind
,并且使用了
std::tr1::result\u of
,这比C++11标准规定的编译器辅助
std::result\u的功能要弱得多

特别是,不要尝试在VS10中对lambda表达式使用
std::bind


您可以通过使用
fopow
inherit
std::bind\u函数
std::bind
来解决这个问题:它实际上是
std::tr1::bind
,并使用
std::tr1::result\u的
,它的功能远远低于VS2010中所要求的编译器辅助的
std::result\u的功能C++11标准

特别是,不要尝试在VS10中对lambda表达式使用
std::bind


您可以通过让
fopow
继承
std::binary_函数

来解决此问题。看起来VC10正在尝试并未能确定绑定的
结果类型
。尝试使
fopow
继承自
std::binary_函数
。看起来VC10正在尝试并未能确定
结果_键入绑定的
。尝试使
fopow
std::binary_函数
继承。它没有中断。它满足TR1的要求,这是在C++11完成之前编写的库的全部要求。@PeteBecker:对于C++11它是中断的(VS没有实现)。让我纠正一下,不要那么粗鲁。嗯,VS 2012是在C++11之后发布的,它确实按照C++11的规定实现了
bind
。感谢您从您的答案中删除了判断性的“it's break”。@PeteBecker:我没有访问VS12的权限,但我想他们已经修复了他们标准库中的“简单bug”(bind、emplace\u back和friends、map::emplace\u hint等)。想象不能代替了解。它没有被破坏。它满足TR1的要求,这是在C++11完成之前编写的库所能要求的一切。@PeteBecker:关于C++11它被破坏了(VS没有实现)。让我纠正一下,不要那么粗鲁。嗯,VS 2012是在C++11之后发布的,它确实按照C++11的规定实现了
bind
。感谢您从您的答案中删除了判断性的“it's break”。@PeteBecker:我没有访问VS12的权限,但我想他们已经修复了他们标准库中的“简单bug”(绑定、放置回和朋友、地图::放置提示等)。想象不能代替了解。+1.你的解决方案有效,但Alexandre C.先回答。谢谢。+1.你的解决方案有效,但Alexandre C.先回答。谢谢。
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

Test2.cpp C:\Program Files\Microsoft Visual Studio
10.0\VC\INCLUDE\xxresult(28) : error C2903: 'result' : symbol is neither a class template nor a function template
        C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xxresult(40) : see reference to class tem plate instantiation 'std::tr1::_Result_type2<__formal,_Fty,_Arg0,_Arg1>' being compiled
        with
        [
            __formal=false,
            _Fty=fopow<int,int>,
            _Arg0=std::tr1::_Nil &,
            _Arg1=std::tr1::_Nil &
        ]
        C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xxresult(597) : see reference to class te mplate instantiation 'std::tr1::_Result_of2<_Fty,_Farg0,_Farg1>' being compiled
        with
        [
            _Fty=fopow<int,int>,
            _Farg0=std::tr1::_Nil &,
            _Farg1=std::tr1::_Nil &
        ]
        C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xrefwrap(28) : see reference to class tem plate instantiation 'std::tr1::_Result_of<_Ty>' being compiled
        with
        [
            _Ty=fopow<int,int> (std::tr1::_Nil &,std::tr1::_Nil &)
        ]
        C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xxbind1(273) : see reference to class tem plate instantiation 'std::tr1::result_of<_Fty>' being compiled
        with
        [
            _Fty=fopow<int,int> (std::tr1::_Nil &,std::tr1::_Nil &)
        ]
        C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xxbind0(10) : see reference to class temp late instantiation 'std::tr1::_Bind2<_Callable,_Arg0,_Arg1>::_Return<_Barg0,_Barg1,_Barg2,_Barg3,_Barg4,_B arg5,_Barg6,_Barg7,_Barg8,_Barg9>' being compiled
        with
        [
            _Callable=std::tr1::_Callable_obj<fopow<int,int>,false>,
            _Arg0=int,
            _Arg1=std::tr1::_Ph<1>,
            _Barg0=std::tr1::_Nil &,
            _Barg1=std::tr1::_Nil &,
            _Barg2=std::tr1::_Nil &,
            _Barg3=std::tr1::_Nil &,
            _Barg4=std::tr1::_Nil &,
            _Barg5=std::tr1::_Nil &,
            _Barg6=std::tr1::_Nil &,
            _Barg7=std::tr1::_Nil &,
            _Barg8=std::tr1::_Nil &,
            _Barg9=std::tr1::_Nil &
        ]
        C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\functional(408) : see reference to class template instantiation 'std::tr1::_Bind_base<_Ret,_BindN>' being compiled
        with
        [
            _Ret=std::tr1::_Notforced,
            _BindN=std::tr1::_Bind2<std::tr1::_Callable_obj<fopow<int,int>,false>,int,std::tr1::_Ph<1>>
        ]
        Test2.cpp(26) : see reference to class template instantiation 'std::tr1::_Bind_fty<_Fty,_Ret,_Bind N>' being compiled
        with
        [
            _Fty=fopow<int,int>,
            _Ret=std::tr1::_Notforced,
            _BindN=std::tr1::_Bind2<std::tr1::_Callable_obj<fopow<int,int>,false>,int,std::tr1::_Ph<1>>
        ] C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xxresult(28) : error C2039: 'result' : is not a m ember of 'fopow<BaseT,ExpT>'
        with
        [
            BaseT=int,
            ExpT=int
        ] C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xxresult(28) : error C2143: syntax error : missin g ';' before '<' C:\Program Files\Microsoft Visual Studio
10.0\VC\INCLUDE\xxresult(28) : error C4430: missing type specifie r - int assumed. Note: C++ does not support default-int C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xxresult(28) : error C2039: 'type' : is not a mem ber of '`global namespace'' C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xxresult(28) : error C2238: unexpected token(s) p receding ';' C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xxresult(40) : error C2039: '_Type' : is not a me mber of 'std::tr1::_Result_type2<__formal,_Fty,_Arg0,_Arg1>'
        with
        [
            __formal=false,
            _Fty=fopow<int,int>,
            _Arg0=std::tr1::_Nil &,
            _Arg1=std::tr1::_Nil &
        ] C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xxresult(40) : error C2146: syntax error : missin g ';' before identifier '_Type' C:\Program Files\Microsoft Visual Studio
10.0\VC\INCLUDE\xxresult(40) : error C4430: missing type specifie r - int assumed. Note: C++ does not support default-int C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xxresult(40) : error C2602: 'std::tr1::_Result_of 2<_Fty,_Farg0,_Farg1>::_Type' is not a member of a base class of 'std::tr1::_Result_of2<_Fty,_Farg0,_Farg1
>'
        with
        [
            _Fty=fopow<int,int>,
            _Farg0=std::tr1::_Nil &,
            _Farg1=std::tr1::_Nil &
        ]
        C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xxresult(40) : see declaration of 'std::t r1::_Result_of2<_Fty,_Farg0,_Farg1>::_Type'
        with
        [
            _Fty=fopow<int,int>,
            _Farg0=std::tr1::_Nil &,
            _Farg1=std::tr1::_Nil &
        ] C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xxresult(40) : error C2868: 'std::tr1::_Result_of 2<_Fty,_Farg0,_Farg1>::_Type' : illegal syntax for using-declaration; expected qualified-name
        with
        [
            _Fty=fopow<int,int>,
            _Farg0=std::tr1::_Nil &,
            _Farg1=std::tr1::_Nil &
        ]