Re: C++11 - friend with a template alias
Am 05.07.2014 05:23, schrieb wojtek.mula@googlemail.com:
Hi, I want to declare alias template as a friend of class. I've tried
following constructions, that work for classes:
-- start --
#include <vector>
template <typename T>
using my_type = std::vector<T>;
class X {
// MSVC 2013 : error C2955: 'my_type' : use of alias template
// requires template argument list
// GCC 4.8.3 : invalid use of template-name 'my_type' without
// an argument list
template <typename> friend my_type;
// MSVC 2013 : error C2988: unrecognizable template declaration/definition
// GCC 4.8.3 : expected unqualified-id before ';' token
template <typename T> friend my_type<T>;
// MSVC 2013 : error C2920: redefinition : 'my_type' : class template has
// already been declared as 'using my_type = std::vector<T,std::allocator<_Ty>>'
// GCC 4.8.3 : declaration of template 'template<class T> struct my_type'
// conflicts with previous declaration 'template<class T>
// using my_type = std::vector<T>'
template <typename T> friend class my_type;
};
Indeed, the language currently does not support alias templates in
friend declarations. According to [temp.friend] p1,
"A friend of a class [..] can be a function template or class template,
a specialization of a function template or class template, or a
non-template function or class."
But your code example is confusing related to your question: Why would
you want to grant friendship to a complete template, and not to a
specific type here?
Aliases aren't mentioned in the section about friends, standard merely says
(section 11.3, paragraph 3):
A friend declaration that does not declare a function shall have one of
the following forms:
friend elaborated-type-specifier ;
friend simple-type-specifier ;
friend typename-specifier ;
What is a valid syntax? Or, maybe it's not possible?
It is possible for concrete types as explained by the grammar, just write
class X {
friend my_type<X>;
};
to declare friendship to essentially std::vector<X>. This works, because
a /simple-type_specifier/ can be a /type-name/, and a /type-name/ can be
a /simple-template-id/ and my_type<X> is one example for the latter.
Warning: If this was intended to be a realistic example, granting
friendship to std::vector<X> is not very helpful in general, because the
implementation of std::vector is allowed to delegate it's code to base
classes or other templates. In particular, several operations such as
construction doesn't happen in the context of std::vector, but instead
somewhere else, such as presumably in code performed by allocator_traits.
I'd like to remark that there exists currently a core language issue
about access within alias templates, see
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1554
HTH & Greetings from Bremen,
- Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]