473,320 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

template specialization, nested?

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

Feb 10 '06 #1
4 1994
Schüle Daniel wrote:
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
--
Please remove capital As from my address when replying by mail
Feb 10 '06 #2
Hello Victor,
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&);


yes, of course you are right
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.


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
void foo<void>(X<type> & x)


isn't it fully specialized?

Regards, Daniel

Feb 12 '06 #3
Schüle Daniel wrote:
[..]
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.
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
void foo<void>(X<type> & x)


isn't it fully specialized?


How would it be if 'type' is unknown?

V
--
Please remove capital As from my address when replying by mail
Feb 12 '06 #4
[..]
void foo<void>(X<type> & x)


isn't it fully specialized?

How would it be if 'type' is unknown?


my minds eye saw only
void foo<void>
:)

thanks for correction

Feb 12 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Andrew Ward | last post by:
Hi All, Considering the following code: struct A; struct B { std::list<A> l; };
2
by: Xenos | last post by:
The new version of GCC is out and in its list of changes, it talks about the C++ Standard's requirements for using the typename and template keywords to disambiguate dependent names. I'm use to...
0
by: Patrick Kowalzick | last post by:
Dear all, the following code is illegeal (but compiles with MSVC 7.1): // *** illegal *** struct outer0 { template<typename inner_var> struct inner { }; template<>
13
by: Imre | last post by:
Please take a look at the following code: template <typename T, typename Enable = void> class A { public: enum { value = 0 }; }; template <typename T>
9
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second...
9
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. ...
2
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
5
by: huili80 | last post by:
For example, like in the following, the part commented out was intended as partial spectialzation, but it would even compile. Is it even legal to partially specialize a nested template class...
2
by: er | last post by:
hello, here's my problem: struct A{}; struct B{}; struct T{}; struct outer{
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.