#cpp #шаблоны_с++ #cpp14
http://ideone.com/Mhpw1y - такой код работает как надо:
struct a
{
template struct b
{
typedef int t;
};
};
template <> struct a::b <1>
{
typedef double t;
};
int main()
{
a::b<0>::t x;
a::b<1>::t y;
return 0;
}
Но я хочу перенести специализацию b внутрь a. Как это сделать?
Следующие способы не работают:
http://ideone.com/3MdjdF
prog.cpp:8:12: error: explicit specialization in non-namespace scope 'struct a'
template <> struct b <1>
^
struct a
{
template struct b
{
typedef int t;
};
template <> struct b <1>
{
typedef double t;
};
};
http://ideone.com/XBD2ga
prog.cpp:8:9: error: too few template-parameter-lists
struct b <1>
^
struct a
{
template struct b
{
typedef int t;
};
struct b <1>
{
typedef double t;
};
};
PS: Связано с этим вопросом.
Ответы
Ответ 1
Согласно стандарту C++ (14.7.3 Explicit specialization) 2 An explicit specialization shall be declared in a namespace enclosing the specialized template. An explicit specialization whose declarator-id is not qualified shall be declared in the nearest enclosing namespace of the template, or, if the namespace is inline (7.3.1), any namespace from its enclosing namespace set. Such a declaration may also be a definition. If the declaration is not a definition, the specialization may be defined later (7.3.1.2).
Комментариев нет:
Отправить комментарий