代码可以用 clang 编译,但不能用 gcc 编译

Code compiles with clang but not with gcc

我有这段代码可以用 clang 编译得很好(即使使用 -Weverything),但是 gcc 会发出错误。

#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

class PhonebookWriter
{
public:

  PhonebookWriter(const string& fname):
    fname_(fname), names_(), numbers_() {}

  PhonebookWriter& operator()(const string& name,
                  const string& number)
  {
    names_.push_back(name);
    numbers_.push_back(number);
    return *this;
  }

  ~PhonebookWriter(void)
  {
    ofstream f(fname_.c_str());
    for(size_t i=0;i<names_.size();++i)
      f << names_[i] << " " << numbers_[i] << "\n";
    f.close();
  }

private:
  const string fname_;
  vector<string> names_;
  vector<string> numbers_;
};

namespace {
  void write_guests_data(const string& fname)
  {
    PhonebookWriter(fname)("Mr Foo Bar","12345")("Mrs Bar Foo","54321");
  }
}

int main(void)
{
  write_guests_data("phone_book.txt");

  return 0;
}

这是我尝试编译代码时得到的结果:

$ g++ ./test.cpp
./test.cpp: In function ‘void {anonymous}::write_guests_data(const string&)’:
./test.cpp:39:27: error: declaration of ‘PhonebookWriter fname’ shadows a parameter
     PhonebookWriter(fname)("Mr Foo Bar","12345")("Mrs Bar Foo","54321");
                           ^
./test.cpp:39:48: error: no matching function for call to ‘PhonebookWriter::PhonebookWriter(const char [11], const char [6])’
     PhonebookWriter(fname)("Mr Foo Bar","12345")("Mrs Bar Foo","54321");
                                                ^
./test.cpp:39:48: note: candidates are:
./test.cpp:11:3: note: PhonebookWriter::PhonebookWriter(const string&)
   PhonebookWriter(const string& fname):
   ^
./test.cpp:11:3: note:   candidate expects 1 argument, 2 provided
./test.cpp:7:7: note: PhonebookWriter::PhonebookWriter(const PhonebookWriter&)
 class PhonebookWriter
       ^
./test.cpp:7:7: note:   candidate expects 1 argument, 2 provided
./test.cpp:39:49: error: expected ‘,’ or ‘;’ before ‘(’ token
     PhonebookWriter(fname)("Mr Foo Bar","12345")("Mrs Bar Foo","54321");
                                                 ^

我的gcc版本是4.9.1,clang版本是3.5.0。 我不明白为什么甚至应该存在阴影问题。就算有,也应该被铛捡到了吧

变化:

PhonebookWriter(fname)("Mr Foo Bar","12345")("Mrs Bar Foo","54321");

至:

(PhonebookWriter(fname))("Mr Foo Bar","12345")("Mrs Bar Foo","54321");

解释

出于某种原因,gcc 删除了 fname 周围的大括号,这会将行变成:

PhonebookWriter fname ("Mr Foo Bar","12345")("Mrs Bar Foo","54321");

现在错误是有道理的。