簡體   English   中英

程序在Visual Studio 2012中運行,但不在ideone.com中運行

[英]Program runs in Visual Studio 2012 but not ideone.com

我有一種直覺VS2012這個錯了,但我不確定。

看完這個問題之后 ,我覺得要嘗試實現類似的東西。

我的版本在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>> candidate函數:

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是正確的,這個代碼是否有任何簡單的解決方法?

第一個模板基本上失敗了,因為你不能將prvalue臨時 - IntSquare () - 綁定到非const左值引用。

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

Ideone(實際上,GCC)在這里是正確的。 在Visual Studio中,它編譯是因為臭名昭着的擴展允許臨時綁定到非const左值引用(標准禁止)。

我在標准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 >>的過載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!
}

當然,理想情況下你也提供混合&, &&&&, &重載。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM