Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1070591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:35:14+00:00 2026-05-16T20:35:14+00:00

This code works: std::ifstream f(mapFilename.c_str()); std::string s = std::string(std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>()); ParseGameState(s); Whereby mapFilename is

  • 0

This code works:

std::ifstream f(mapFilename.c_str());
std::string s = std::string(std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>());
ParseGameState(s);

Whereby mapFilename is an std::string and void ParseGameState(const std::string&);.

And this does not:

std::ifstream f(mapFilename.c_str());
std::string s(std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>());
ParseGameState(s);

This is the error:

game.cpp: In member function ‘int Game::LoadMapFromFile(const std::string&)’:
game.cpp:423: error: no matching function for call to ‘ParseGameState(std::string (&)(std::istreambuf_iterator<char, std::char_traits<char> >, std::istreambuf_iterator<char, std::char_traits<char> > (*)()))’
game.cpp:363: note: candidates are: ParseGameState(const std::string&)

So it seems that it recognizes s as a function declaration and not a variable declaration in this case.

Why is that? Is this a bug in GCC 4.2.1 (Apple build)? Or does GCC handles this correctly? Is this undefined in the C++ standard?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:35:15+00:00Added an answer on May 16, 2026 at 8:35 pm

    This is C++’s “most vexing parse.” A quick Google for that should turn up lots of hits with lots of details. The basic answer is that yes, the compiler is treating it as a function declaration — and C++ requires that it do so. There’s nothing wrong with your compiler (at least in this respect).

    If it’s any comfort, you have lots of good company in having run into this. In fact, it’s sufficiently common that C++0x is added a new brace-initializer syntax, in large part because it avoids this ambiguity. Using it, you could write something like:

    std::string s{std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>()};
    

    That will make it clear that the contents of the braces are intended to be values for initializing s, not types of parameters to a function named s. I don’t know if Apple has a port of it yet, but gcc accepts the new syntax as of version 4.5 (or so).

    Edit: Rereading N3092, Johannes is (as usual) quite correct. The applicable language is (§8.5.4/3/5): “If T has an initializer-list constructor, the argument list consists of the initializer list as a single argument; otherwise, the argument list consists of the elements of the initializer list.”

    So, since std::string has an initializer-list constructor, this would attempt to “stuff” the two istreambuf_iterators into an initializer list, and pass that to the std::string ctor that takes an initializer list — but that would be a type mismatch, so the code can’t compile. For some other type type that (unlike std::string did not have an initializer-list ctor) the transformation above would work (thanks to the “otherwise…” in the quote above). In the case of std::string, you’d have to use one of the current alternatives such as std::string s = std:string(...).

    I apologize for the incorrect suggested fix — in this case, one that’s all the worse because it confuses an issue that will probably be excessively confusing on its own, and if anything will need careful clarification, especially over the next few years.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A colleague just told me that this code: std::ifstream stream(filename.c_str()); if (!stream) { throw
This code works (C# 3) double d; if(d == (double)(int)d) ...; Is there a
This code works fine: $result = $client->__call(optionalInfo, array( new SoapParam(..., client), new SoapParam(..., add_code))
This code works, but i dont understand why. With DeferredLoadingEnabld = false, I would
This code works in a windows forms application (it shows the preview) but not
I am using ASP.NET to transmit a .jar file. This code works perfectly on
This code always works, even in different browsers: function fooCheck() { alert(internalFoo()); // We
I have this code, which works fine, but I would like to be able
This is kind of a brainteaser question, since the code works perfectly fine as-is,
I'm trying to use this code to replace spaces with _, it works for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.