2014-10-07 80 views
6

我有一种直觉VS2012在这一个上是错误的,但我不确定。程序在Visual Studio 2012中运行,但不是ideone.com

看过this question之后,我觉得想要实现类似的东西。

我的版本在Visual Studio 2012上工作正常,但是甚至没有在Ideone上编译。

这里是我的主界面:

#include <iostream> 
#include <string> 

template <class In, class Out> 
struct Pipe 
{ 
    typedef In in_type ; 
    typedef Out out_type ; 

    In in_val ; 

    Pipe (const in_type &in_val = in_type()) : in_val (in_val) 
    { 
    } 

    virtual auto operator()() const -> out_type 
    { 
     return out_type() ; 
    } 
}; 

template <class In, class Out, class Out2> 
auto operator>> (const Pipe <In, Out> &lhs, Pipe <Out, Out2> &rhs) -> Pipe <Out, Out2>& 
{ 
    rhs = lhs() ; 
    return rhs ; 
} 

template <class In, class Out> 
auto operator>> (const Pipe <In, Out> &lhs, Out &rhs) -> Out& 
{ 
    rhs = lhs() ; 
    return rhs ; 
} 

这里有几个测试类:

struct StringToInt : public Pipe <std::string, int> 
{ 
    StringToInt (const std::string &s = "") : Pipe <in_type, out_type> (s) 
    { 
    } 

    auto operator()() const -> out_type 
    { 
     return std::stoi (in_val) ; 
    } 
}; 

struct IntSquare : public Pipe <int, int> 
{ 
    IntSquare (int n = 0) : Pipe <in_type, out_type> (n) 
    { 
    } 

    auto operator()() const -> out_type 
    { 
     return in_val * in_val ; 
    } 
}; 

struct DivideBy42F : public Pipe <int, float> 
{ 
    DivideBy42F (int n = 0) : Pipe <in_type, out_type> (n) 
    { 
    } 

    auto operator()() const -> out_type 
    { 
     return static_cast <float> (in_val)/42.0f ; 
    } 
}; 

和这里的司机:

int main() 
{ 
    float out = 0 ; 
    StringToInt ("42") >> IntSquare() >> DivideBy42F() >> out ; 
    std::cout << out << "\n" ; 

    return 0 ; 
} 

Ideone抱怨模板扣除和其无法找到正确的operator>>候选功能:

prog.cpp: In function ‘int main()’: 
prog.cpp:75:21: error: no match for ‘operator>>’ (operand types are ‘StringToInt’ and ‘IntSquare’) 
    StringToInt ("42") >> IntSquare() >> DivideBy42F() >> out ; 
        ^
prog.cpp:75:21: note: candidates are: 
prog.cpp:23:6: note: Pipe<Out, Out2>& operator>>(const Pipe<In, Out>&, Pipe<Out, Out2>&) [with In = std::basic_string<char>; Out = int; Out2 = int] 
auto operator>> (const Pipe <In, Out> &lhs, Pipe <Out, Out2> &rhs) -> Pipe <Out, Out2>& 
    ^
prog.cpp:23:6: note: no known conversion for argument 2 from ‘IntSquare’ to ‘Pipe<int, int>&’ 
prog.cpp:30:6: note: template<class In, class Out> Out& operator>>(const Pipe<In, Out>&, Out&) 
auto operator>> (const Pipe <In, Out> &lhs, Out &rhs) -> Out& 
    ^
prog.cpp:30:6: note: template argument deduction/substitution failed: 
prog.cpp:75:35: note: deduced conflicting types for parameter ‘Out’ (‘int’ and ‘IntSquare’) 
    StringToInt ("42") >> IntSquare() >> DivideBy42F() >> out ; 

哪个编译器是正确的?如果Ideone是正确的,是否有任何简单的解决这个代码?

+3

与(*可能重复?*)相关:[非常量引用绑定到临时的Visual Studio错误?](http://stackoverflow.com/q/16380966/1708801)...基本上gcc在这里是正确的。如果在Visual Studio中使用'/ Za'构建,它也会失败,[请参阅此链接](http://rextester.com/GYMOJ23021)。 – 2014-10-07 20:36:17

+3

这是愚蠢的投票结束这个问题,因为*为什么不是这个代码工作*。测试可能会更小,但它是一个完整的测试用例,编译器之间的结果相互矛盾,并且对于大多数人来说这肯定不明显。 – 2014-10-07 20:43:37

回答

2

Ideone(实际上,GCC)在这里是正确的。在Visual Studio中,它是由于臭名昭着的扩展而编译的,它允许临时绑定到非常量左值引用(标准禁止)。

我看到几个可能的方式在标准C来解决这个++:

一,不使用临时变量的流水线阶段:

int main() 
{ 
    float out = 0 ; 
    StringToInt stage1("42"); 
    IntSquare stage2; 
    DivideBy24F stage3; 
    stage1 >> stage2 >> stage3 >> out ; 
    std::cout << out << "\n" ; 

    return 0 ; 
} 

二,创建一个“留”功能(相对std::move),并使用该:

template <class T> 
T& stay(T &&x) { return x; } 

int main() 
{ 
    float out = 0 ; 
    stay(StringToInt ("42")) >> stay(IntSquare()) >> stay(DivideBy42F()) >> out ; 
    std::cout << out << "\n" ; 

    return 0 ; 
} 

三,提供的operator >>过载服用r值参考:

template <class In, class Out, class Out2> 
auto operator>> (const Pipe <In, Out> &&lhs, Pipe <Out, Out2> &&rhs) -> Pipe <Out, Out2>& 
{ 
    return lhs >> rhs; // Notice that lhs and rhs are lvalues! 
} 

当然,理想情况下,您也可以提供混合的&, &&&&, &重载。

2

第一个模板基本上失败了,因为您无法将临时值 - IntSquare() - 绑定到非常量左值引用。

no known conversion for argument 2 from ‘IntSquare’ to ‘Pipe<int, int>&’ 

这是说,你不能用IntSquare型prvalue初始化Pipe<int, int>&。不幸的是,错误消息中没有明确提到值类别。尽管这是一个标准规则,VC++却忽略了它来缓解(或者麻烦)C++程序员的日常生活。

第二模板

template <class In, class Out> 
auto operator>> (const Pipe <In, Out> &lhs, Out &rhs) -> Out& 
{ 
    rhs = lhs() ; 
    return rhs ; 
} 

失败Out两种不同类型的,因为两个不同的扣除,推导 - 第一个是int(对于lhs),第二个是IntSquare

+0

+1我发现两个答案都有帮助,但是我和其他人一起去了。 – jliv902 2014-10-07 21:07:14

相关问题