我有下面的代碼在g ++下編譯,但不是與鏗鏘編譯。這是有效的C++ 11
如果以各種次要方式更改代碼(如合併2個名稱空間聲明),Clang將編譯代碼。
// The problem disappears without namespaces.
namespace Root {
// The problem disappears if 'g' is in the global namespace, and we change
// the friend declaration to '::g'
// The problem disappears if 'g' has void return type.
// The problem disappears if we get rid of the 'Value' template argument
// and the 'value' parameter.
template<typename Value, typename Defaulted = void>
bool g(Value value);
// The problem disappears if MyClass is not a template.
template<typename ClassValue>
class MyClass {
private:
template<typename Value, typename Defaulted>
friend bool g(Value value);
};
}
// The problem disappears if we declare the Root namespace in a single block
// containing 'g', 'MyClass' and 'f'.
// The problem remains if we declare f in the global namespace and reference
// Root::g.
namespace Root {
void f() {
MyClass<int> value;
g(value);
}
}
要使用鐺編譯:
clang -fsyntax-only -std=c++11 testcase.cpp
要使用克++編譯:
g++ -fsyntax-only -std=c++11 testcase.cpp
版本克++ 4.9.2,3.6.0鐺,既在Ubuntu芯15.04。
鏘給人的錯誤消息:
testcase.cpp:24:9: error: no matching function for call to 'g'
g(value);
^
testcase.cpp:14:21: note: candidate template ignored: couldn't infer template argument 'Defaulted'
friend bool g(Value value);
^
1 error generated.
友元函數(即使在一個類中聲明)的命名空間範圍。所以在這種情況下,你有兩個'bool Root :: g()'函數聲明。模板參數不會更改聲明。這就是爲什麼你的小改動使這個代碼有效。我實際上更驚訝,這與g ++編譯。 –
@SimonKraemer您可以多次聲明函數。無論如何,「朋友」聲明不會重新聲明函數。 – Barry
這是您的完整代碼或只是相關的部分? –