template specialization, nested?

V

Victor Bazarov

Schüle Daniel said:
my question is basically wheather it's possible to
have nested templates .. I mean the following
if there are one templ. class and one global templ. function

is it possible to specialize the function for say <int>
but this for all possible classes (from class template)

Uh... I am not sure I understand what you're trying to accomplish.
the code is here

template <typename>
class X;

template <typename type>
void foo(X<type> &);

template <typename type = int>
class X
{
friend void foo<type>();

There is no 'foo' without arguments! Did you mean

friend void foo<type>(X&);

?
type x;
public:
X(type x) : x(x) {}
};

template <typename type>
void foo(X<type> & x)
{
cout << "with type = " << typeid(type).name() << endl;
cout << x.x << endl;
}

template <> // this seems not ok
template <typename type> // but i need "type" for X

OK. So, you're trying to define a specialisation of 'foo' on 'void'
so that the argument is now a template. IOW, you're defining another
template here... That's not possible. Besides, there are no partial
specialisations of function templates.

Why do you think you need this construct?
void foo<void>(X<type> & x)
{
cout << "this is void overload" << endl;
cout << x.x << endl;
}

int main()
{
X<> x(1); // int
foo<void>(x);
return EXIT_SUCCESS;
}

Regards, Daniel

g++ --version
g++ (GCC) 4.0.2

V
 
?

=?ISO-8859-1?Q?Sch=FCle_Daniel?=

Hello all,

my question is basically wheather it's possible to
have nested templates .. I mean the following
if there are one templ. class and one global templ. function

is it possible to specialize the function for say <int>
but this for all possible classes (from class template)

the code is here

template <typename>
class X;

template <typename type>
void foo(X<type> &);

template <typename type = int>
class X
{
friend void foo<type>();
type x;
public:
X(type x) : x(x) {}
};

template <typename type>
void foo(X<type> & x)
{
cout << "with type = " << typeid(type).name() << endl;
cout << x.x << endl;
}

template <> // this seems not ok
template <typename type> // but i need "type" for X
void foo<void>(X<type> & x)
{
cout << "this is void overload" << endl;
cout << x.x << endl;
}

int main()
{
X<> x(1); // int
foo<void>(x);
return EXIT_SUCCESS;
}

Regards, Daniel

g++ --version
g++ (GCC) 4.0.2
 
V

Victor Bazarov

Schüle Daniel said:
[..]
something like

template <typename type>
void foo() {}

template <>
void foo<int>(){}

my understanding of it is, that this is fully specialized
and is ok, my compiler at least doesn't complain

Yes. Fully specialised function templates are OK.
I thought it's time to get familar with template usage
(to write libraries)
so mostly this an academic case


isn't it fully specialized?

How would it be if 'type' is unknown?

V
 
?

=?ISO-8859-1?Q?Sch=FCle_Daniel?=

Hello Victor,
There is no 'foo' without arguments! Did you mean

friend void foo<type>(X&);

yes, of course you are right
OK. So, you're trying to define a specialisation of 'foo' on 'void'
so that the argument is now a template. IOW, you're defining another
template here... That's not possible. Besides, there are no partial
specialisations of function templates.

something like

template <typename type>
void foo() {}

template <>
void foo<int>(){}

my understanding of it is, that this is fully specialized
and is ok, my compiler at least doesn't complain
Why do you think you need this construct?
I thought it's time to get familar with template usage
(to write libraries)
so mostly this an academic case

isn't it fully specialized?

Regards, Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top