构造函数(C ++)中的临时非const istream引用 [英] Temporary non-const istream reference in constructor (C++)

查看:213
本文介绍了构造函数(C ++)中的临时非const istream引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎一个构造函数对一个istream的非const引用不能用C ++中的临时值来构造。

  #include< iostream> 
#include< sstream>

using namespace std;

class Bar
{
public:
explicit Bar(std :: istream& is){}
};

int main()
{
istringstream stream1(bar1);
Bar bar1(stream1); // OK在所有平台上

//在linux,Mac上编译错误gcc;在Windows上OK OK
bar bar2(istringstream(bar2));

return 0;
}

这与MSVC编译良好,但不与gcc。使用gcc我得到一个编译错误:

  g ++ test.cpp -o test 
test.cpp: int main()':
test.cpp:18:error:没有匹配函数调用'Bar :: Bar(std :: istringstream)'
test.cpp:9: :Bar :: Bar(std :: istream&)
test.cpp:7:note:Bar :: Bar(const Bar&)

构造一个Bar对象的第二种方式(bar2)有什么哲学上的错误吗?

编辑:为了响应Johannes Schaub的调用,我需要一个只需要一会儿的stream1变量。



<评论我想给一些更多的上下文。首先,这不是我第一次对C ++的这种行为感到烦恼,所以我对这个问题的更高层次的哲学讨论真正感兴趣。也就是说,在这种特殊情况下,我有一个类,它读入一个文件,其中包含用于构造对象的数据。我也喜欢编写使用字符串而不是文件的自动化测试。但是使用文件构造是主要的用例。所以我决定做一个构造函数,它需要一个istream,所以我可以使用文件(流)或字符串(流)。这就是我在这里。我的测试程序直接从字符串构造对象,以模拟读取文件。这可以节省我为每个小测试创建单独的数据文件的麻烦。

解决方案

这是C ++的工作原理:将非const引用绑定到临时对象。 MSVC是允许这种情况的非标准。



C ++ 0x将有r值引用,并在这里改变一些东西。有很多哲学解释,人们试图应用于问题的双方;但我没有找到一个完全令人信服的。它似乎更多的你只需要选择一个行为,坚持,这解释了当前的C ++和0x的变化:所选择的行为已经改变。


It seems that a constructor that takes a non-const reference to an istream cannot be constructed with a temporary value in C++.

#include <iostream>
#include <sstream>

using namespace std;

class Bar
{
public:
   explicit Bar(std::istream& is) {}
};

int main()
{
   istringstream stream1("bar1");
   Bar bar1(stream1); // OK on all platforms

   // compile error on linux, Mac gcc; OK on Windows MSVC
   Bar bar2(istringstream("bar2"));

   return 0;
}

This compiles fine with MSVC, but not with gcc. Using gcc I get a compile error:

g++     test.cpp   -o test
test.cpp: In function ‘int main()’:
test.cpp:18: error: no matching function for call to ‘Bar::Bar(std::istringstream)’
test.cpp:9: note: candidates are: Bar::Bar(std::istream&)
test.cpp:7: note:                 Bar::Bar(const Bar&)

Is there something philosophically wrong with the second way (bar2) of constructing a Bar object? It looks nicer to me, and does not require that stream1 variable that is only needed for a moment.

EDIT: In response to Johannes Schaub's comment I'd like to give a bit more context. First, this is not the first time I have been annoyed by this behavior of C++, so I am genuinely interested in the higher level philosophical discussion of this issue. That said, in this particular case I have a class that reads in a file that contains data used to construct the object. I also like to write automated tests that use a string instead of the file. But using the file for construction is the primary use case. So I decided to make a constructor that takes an istream, so I could use either a file(stream), or a string(stream). That is how I got here. My test programs construct objects directly from strings, to simulate reading files. This saves me the trouble of creating separate data files for each little test.

解决方案

This is just how C++ works currently: you cannot bind non-const references to temporary objects. MSVC is non-standard in allowing this.

C++0x will have r-value references and change things around a bit here. There are various philosophical interpretations people have tried to apply—for both sides of the issue—but I haven't found one that is wholly convincing. It seems more of "you just have to pick one behavior and stick to it", which explains both current C++ and 0x's changes: the chosen behavior has shifted.

这篇关于构造函数(C ++)中的临时非const istream引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆