从临时流中提取字符时 GCC 编译器错误

发布于 2024-12-28 21:47:46 字数 487 浏览 2 评论 0原文

我正在尝试从流中读取单个字符。使用以下代码,我收到“不明确的重载”编译器错误(GCC 4.3.2、和 4.3.4)。我做错了什么?

#include <iostream>
#include <sstream>

int main()
{
    char c;
    std::istringstream("a") >> c;
    return 0;
}

备注:

  • Visual Studio 2008 编译没有错误
  • 其他类型(intdouble)可以工作
  • 如果我首先创建一个变量 std::istringstream iss("a") ;国际空间站>> c,编译器没有报错

I'm trying to read a single character from a stream. With the following code I get a "ambiguous overload" compiler error (GCC 4.3.2, and 4.3.4). What I'm doing wrong?

#include <iostream>
#include <sstream>

int main()
{
    char c;
    std::istringstream("a") >> c;
    return 0;
}

Remarks:

  • Visual Studio 2008 compiles without errors
  • Other types (int, double) are working
  • If I first create a variable std::istringstream iss("a"); iss >> c, the compiler gives no error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怎樣才叫好 2025-01-04 21:47:46

字符的提取运算符 >> 是非成员函数模板:

template<class charT, class traits>
  basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>&, charT&);

由于它通过非 const 引用获取其第一个参数,因此您不能使用临时那里有右值。因此,您的代码无法选择此重载,只能选择各种成员函数重载,其中没有一个与此用法匹配。

您的代码在 C++11 中有效,因为还有一个提取运算符将右值引用作为第一个参数。

Visual Studio 2008 编译没有错误

该编译器的许多非标准扩展之一是允许临时右值绑定到非const引用。

其他类型(intdouble)正在工作

大多数基本类型的提取运算符都是成员函数,可以在临时上调用右值

如果我首先创建一个变量 std::istringstream iss("a");国际空间站>> c,编译器没有报错

iss 是一个非临时左值,因此它可以绑定到非const参考。

The extraction operator >> for characters is a non-member function template:

template<class charT, class traits>
  basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>&, charT&);

Since this takes its first argument by non-const reference, you can't use a temporary rvalue there. Therefore, your code cannot select this overload, only the various member function overloads, none of which match this usage.

Your code is valid in C++11, because there is also an extraction operator taking an rvalue reference as the first argument.

Visual Studio 2008 compiles without errors

One of that compiler's many non-standard extensions is to allow temporary rvalues to be bound to non-const references.

Other types (int, double) are working

Most extraction operators for fundamental types are member functions, which can be called on a temporary rvalue.

If I first create a variable std::istringstream iss("a"); iss >> c, the compiler gives no error

iss is a non-temporary lvalue, so it can be bound to a non-const reference.

农村范ル 2025-01-04 21:47:46

读取 charoperator>> 的签名是

template<class charT, class traits>
basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>& in,charT& c);

根据语言规则,临时变量不能绑定到第一个参数,因为临时变量不能绑定到非常量参考。

Visual Studio 2008 允许将其作为 MS 扩展。以后的版本会警告你这是不允许的。

The signature for the operator>> reading a char is

template<class charT, class traits>
basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>& in,charT& c);

According to the language rules, a temporary cannot bind to the first parameter as a temporary cannot bind to a non-const reference.

Visual Studio 2008 allows this as an MS extension. Later versions will warn you that it is not allowed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文