Чем Clang отличается от GCC в том, как он находит объявления - PullRequest
0 голосов
/ 12 марта 2012

У меня есть util.h / .cpp с перегрузкой оператора >> для istreams, которая выглядит как

// util.h
//
// ... lots of stuff ...

std::istream& operator>>(std::istream& is, const char *str);
std::istream& operator>>(std::istream& is, char *str);

И

// util.cpp
//
// lots of stuff again
//! a global operator to scan (parse) strings from a stream
std::istream& operator>>(std::istream& is, const char *str){
  parse(is, str); return is;
}
//! the same global operator for non-const string
std::istream& operator>>(std::istream& is, char *str){
  parse(is, (const char*)str); return is;
}

в каком-то другом файле, которым я пользуюсьпостроить так:

std::istream file;
char *x, *y;

// opening and allocating space for strings comes here

file >> "[ " >> x >> "," >> y >> " ]";

Это прекрасно работало с gcc / g ++ (v. 4.6.3), но теперь я хотел использовать clang (v 3.0) и получил ошибки, утверждающие, что перегрузка соответствующего оператора можетне найден:

clang -ferror-limit=1 -g -Wall -fPIC -o ors.o -c ors.cpp
ors.cpp:189:21: error: invalid operands to binary expression ('std::istream' (aka 'basic_istream<char>') and 'const char [2]')
   file >> "[ " >> x >> "," >> y >> " ]";
   ~~~~ ^ ~~~~
/usr/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.6.3/../../../../include/c++/4.6.3/istream:121:7: note: candidate function
  not viable: no known conversion from 'const char [2]' to '__istream_type &(*)(__istream_type &)' for 1st argument;
  operator>>(__istream_type& (*__pf)(__istream_type&))
 [[ lots of other possible candidates from the stl ]]

Почему clang не может найти соответствующее объявление, в то время как у gcc нет проблем.Как я могу это исправить?

1 Ответ

2 голосов
/ 13 марта 2012

Вы уверены, что включаете util.h в файл, где вы делаете 'file >> "["'?

Без подробностей трудно сказать, с какой проблемой вы сталкиваетесь. Для меня clang прекрасно компилируется со следующей полной программой:

#include <iostream>
#include <sstream>

std::istream& operator>>(std::istream& is, const char *str);
std::istream& operator>>(std::istream& is, char *str);

int main() {
    std::stringstream file("tmp");
    char *x, *y;

    // opening and allocating space for strings comes here

    file >> "[ " >> x >> "," >> y >> " ]";
}


// util.cpp
//
// lots of stuff again

void parse(std::istream& is, const char *str) {}

//! a global operator to scan (parse) strings from a stream
std::istream& operator>>(std::istream& is, const char *str){
    parse(is, str); return is;
}
//! the same global operator for non-const string
std::istream& operator>>(std::istream& is, char *str){
    parse(is, (const char*)str); return is;
}

Хотя вам следует подумать о том, чтобы сделать это каким-то другим способом, поскольку предоставление собственного перегруженного оператора для переопределения стандартного оператора не является хорошей практикой. Правила поиска тайны, и код, который их злоупотребляет, вероятно, будет трудно понять и поддерживать.

...