472,811 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,811 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 1960
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{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.