Mam następujący kod, który kompiluje się pod g ++, ale nie z clang.Czy to jest prawidłowy C++ 11
Clang skompiluje kod, jeśli zostanie zmieniony na różne drobne sposoby, na przykład scalenie 2 deklaracji przestrzeni nazw.
// 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);
}
}
Kompilacja z brzękiem:
clang -fsyntax-only -std=c++11 testcase.cpp
Aby podczas kompilacji g ++
g++ -fsyntax-only -std=c++11 testcase.cpp
są wersje g ++ 4.9.2, 3.6.0 szczęk, zarówno w rdzeniu Ubuntu 15,04.
Clang daje komunikat o błędzie:
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.
Funkcja przyjaciel (nawet jeśli zadeklarowana w klasie) ma zakres przestrzeni nazw. Tak więc w tym przypadku masz 2 deklaracje funkcji dla 'bool Root :: g()'. Parametry szablonu nie zmieniają deklaracji. Właśnie dlatego twoje drobne zmiany sprawiają, że ten kod działa. Jestem bardziej zaskoczony, że kompiluje się z g ++. –
@SimonKraemer Możesz zadeklarować funkcje wiele razy. A deklaracja "przyjaciela" i tak nie odtworzy tej funkcji. – Barry
Czy to Twój pełny kod, czy tylko odpowiednia część? –