Посмотрите на следующий код, пожалуйста.Я попытался скомпилировать его на https://wandbox.org/. Он может быть скомпилирован с GCC-8.2.0, но не с GCC-6.3.0 (сообщение об ошибке error: 'key<owner>::key() [with owner = T1]' is private within this context
).Я думаю, что поведение GCC-6.3.0 является правильным, и сообщение об ошибке - именно то, что я хочу видеть.Так чего же мне не хватает?Кажется, существование = {}
в T(int i, key<owner> = {})
вызывает эту проблему.Но я хотел бы знать причину этого.Есть ли неопределенное поведение, и последние версии компиляторов трактуют его по-разному?
#include <iostream>
#include <string>
template<class owner>
struct key {
friend owner;
private:
key(){}
key(key const& )=default;
key(key &&)=default;
};
template<class owner>
struct T {
T(int i, key<owner> = {}) { std::cout << i << " (not-important)\n"; }
void fun() {
T(1); // If uncommented then it should not be possible to compile the code
std::cout << "void fun (not-important)\n"; }
};
struct T1 {
static T<T1> fun() { return T<T1>(3); };
};
int main() {
T<T1>(2); // If uncommented then it should not be possible to compile the code
T<T1> t = T1::fun();
t.fun();
}