简体   繁体   English

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

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

I have a gut-feeling VS2012 is wrong on this one, but I'm not certain. 我有一种直觉VS2012这个错了,但我不确定。

After looking this question , I felt like trying to implement something similar. 看完这个问题之后 ,我觉得要尝试实现类似的东西。

My version works fine on Visual Studio 2012, but does not even compile on Ideone . 我的版本在Visual Studio 2012上运行良好,但甚至不在Ideone上编译。

Here is my main interface: 这是我的主界面:

#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 ;
}

Here are a few test classes: 以下是一些测试类:

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 ;
    }
};

And here's the driver: 这是驱动程序:

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

    return 0 ;
}

Ideone is complaining about template deductions and its unable to find the correct operator>> candidate function: 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 ;

Which compiler is correct? 哪个编译器正确? If Ideone is correct, is there any easy fix to this code? 如果Ideone是正确的,这个代码是否有任何简单的解决方法?

The first template basically fails because you can't bind a prvalue temporary - IntSquare () - to a non-const lvalue reference. 第一个模板基本上失败了,因为你不能将prvalue临时 - IntSquare () - 绑定到非const左值引用。

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

This says that you can't initialize Pipe<int, int>& with a prvalue of type IntSquare . 这表示您无法使用IntSquare类型的prvalue初始化Pipe<int, int>& The value category is unfortunately not explicitly mentioned in the error message. 遗憾的是,错误消息中未明确提及值类别。 Although this is a standard rule VC++ ignores it to ease (or troublesome) a C++-programmers daily life. 虽然这是一个标准规则,VC ++忽略了它来缓解(或麻烦)C ++ - 程序员的日常生活。

The second template 第二个模板

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

fails because for two different deductions of Out two different types were deduced - the first being int (for lhs ) and the second being IntSquare . 失败是因为两个不同的推论Out两个不同的类型 - 第一个是int (对于lhs ),第二个是IntSquare

Ideone (actually, GCC) is correct here. Ideone(实际上,GCC)在这里是正确的。 In Visual Studio, it compiles because of an infamous extension which allows temporaries to bind to non-const lvalue references (the standard forbids that). 在Visual Studio中,它编译是因为臭名昭着的扩展允许临时绑定到非const左值引用(标准禁止)。

I see several possible ways to solve this in standard C++: 我在标准C ++中看到了几种可能的解决方法:

One, don't use temporaries for the pipeline stages: 一,不要使用临时管道阶段:

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

    return 0 ;
}

Two, create a "stay" function (opposite of std::move ), and use that: 二,创建一个“停留”功能(与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 ;
}

Three, provide an overload of operator >> taking an r-value reference: 三,提供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!
}

Of course, ideally you'd provide mixed &, && and &&, & overloads as well. 当然,理想情况下你也提供混合&, &&&&, &重载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么ideone.com这样做? - Why ideone.com does this? 为什么这个C ++代码在ideone.com上获得SIGILL? - Why does this C++ code get SIGILL on ideone.com? C++ 正则表达式在 ideone.com 上匹配,但在 Android NDK 构建中不匹配 - C++ regex matches on ideone.com but not in Android NDK build 这段代码可以在代码块中完美运行,但在ideone.com上给出了运行时错误 - This code is running perfectly in codeblocks but giving a runtime error on ideone.com 如何为ideone.com中的代码文件指定自定义文件名? - How can I give a custom file names to a code file in ideone.com? Visual Studio - 分析时程序运行速度更快 - Visual Studio - Program runs faster when profiling 程序在Visual Studio中运行,但释放可执行文件崩溃 - Program runs in visual studio but release executable crashes 当我在ideone.com上运行时,在阶乘中尾随零。 甚至SPOJ也不接受 - trailing zeros in a factorial.when i run it on ideone.com i'm getting runtime error with infinite output. Even SPOJ is also not accepting 如何在Visual Studio 2012中创建HelloWorld COM互操作 - How to create a HelloWorld COM Interop in Visual Studio 2012 为什么这会在ideone和visual studio中显示运行时错误,但在代码块中却没有? - why this shows runtime error in ideone & visual studio but not in code blocks?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM