У меня есть шаблонный шаблон A
и шаблонная функция f
, которая возвращает A
объектов. Я хочу f<T>
быть другим A<T>
и по-прежнему constexpr
Объявление специалиста constexpr как друга
template <typename T>
class A;
template <typename T>
constexpr A<T> f();
//f<T> is a friend of A<T>
template <typename T>
class A {
friend /* constexpr? */ A f<T>();
constexpr A() {}
};
template <typename T>
constexpr A<T> f() { return {}; }
int main() {
constexpr auto a = f<void>();
}
Я не могу получить лязг и GCC, чтобы договориться о том, что прямо здесь. Если я не ставлю constexpr
в другом заявлении, НКУ работает нормально, но лязг не будет компилировать его, erroring с:
main.cpp:18:18: error: constexpr variable 'a' must be initialized by a constant expression
constexpr auto a = f<void>();
^ ~~~~~~~~~
main.cpp:18:23: note: non-constexpr function 'f<void>' cannot be used in a constant expression
constexpr auto a = f<void>();
^
main.cpp:9:12: note: declared here
friend A f<T>();
Если я пометить его как constexpr
в другом заявлении, лязг отлично компилируется, но НКУ дает me error:
main.cpp:9:27: error: 'constexpr' is not allowed in declaration of friend template specialization 'A<T> f<T>()'
friend constexpr A f<T>();
Как я могу сделать всех счастливыми?
см. [Этот вопрос и ответы] (http://stackoverflow.com/questions/29871138/constexpr-is-not-allowed-in-declaration-of-friend-template-specialization) для аналогичной ошибки – TemplateRex