Solved

Follow-up: Finding template parameter types

Posted on 2009-07-12
11
254 Views
Last Modified: 2012-05-07
Hi,

This is a follow up from:
http://www.experts-exchange.com/Q_24563512.html

Evilrix posted a solution which gets resolved at compile time, although the if statements appear within the function foo(), how can different function defenitions be created at compile-time based on the parameters put into the template - or perhaps a better question, how does the Boost signals library manage to achieve creating different function defintions for the callbacks?

Thanks,
Uni
0
Comment
Question by:Unimatrix_001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 24834854
hi Uri. Out having food right now. I'll get back to you when I return :)
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 24834861
No probs - enjoy yourself. :)
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24835546
Ok, see what you make of the code below...
#include <iostream>
 
struct NoArg {};
 
template<typename A, typename B = NoArg, typename C = NoArg>
class CMyClass
{
public:
	void foo(A, B, C)
	{
		std::cout << "void foo(A, B, C)" << std::endl;
	}
};
 
template<typename A, typename B >
class CMyClass<A, B, NoArg>
{
public:
	void foo(A, B)
	{
		std::cout << "void foo(A, B)" << std::endl;
	}
};
 
template<typename A>
class CMyClass<A, NoArg, NoArg>
{
public:
	void foo(A)
	{
		std::cout << "void foo(A)" << std::endl;
	}
};
 
 
int main()
{
        CMyClass<int, char, float> c1;
        c1.foo(1, 'c', 2.34);
        
        CMyClass<int, char> c2;
        c2.foo(1, 'c');
 
        CMyClass<int> c3;
        c3.foo(1);
}

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:Unimatrix_001
ID: 24835567
Ah okay - I see... How would things fare if we had something like this for example:
template<typename A, typename B = NoArg, typename C = NoArg, typename D = std::allocator<A> >
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24835635
>> How would things fare if we had something like this for example:
Arrr... brain hurts..... arrrg! :)



#include <iostream>
 
struct NoArg {};
 
template<typename A, typename B = NoArg, typename C = NoArg, typename D = std::allocator<A> >
class CMyClass
{
public:
	void foo(A, B, C)
	{
		std::cout << "void foo(A, B, C)" << std::endl;
	}
};
 
template<typename A, typename B >
class CMyClass<A, B, NoArg>
{
public:
	void foo(A, B)
	{
		std::cout << "void foo(A, B)" << std::endl;
	}
};
 
template<typename A>
class CMyClass<A, NoArg, NoArg>
{
public:
	void foo(A)
	{
		std::cout << "void foo(A)" << std::endl;
	}
};
 
 
int main()
{
        CMyClass<int, char, float, std::allocator<double> > c0;
	
        CMyClass<int, char, float> c1;
        c1.foo(1, 'c', 2.34);
        
        CMyClass<int, char> c2;
        c2.foo(1, 'c');
 
        CMyClass<int> c3;
        c3.foo(1);
}

Open in new window

0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 24835643
>>Arrr... brain hurts..... arrrg! :)
Hehe, I'm nearly done. ;)

The thing with that is the last CMyClass:

--------------------------------------------------------------------------------------------------------
template<typename A>
class CMyClass<A, NoArg, NoArg>
{
public:
        void foo(A)
        {
                std::cout << "void foo(A)" << std::endl;
        }
};
--------------------------------------------------------------------------------------------------------

doesn't actually know anything about the D typename, so could never use this - correct?
0
 
LVL 40

Accepted Solution

by:
evilrix earned 500 total points
ID: 24835650
>> correct?
Yes. Each class is, in effect, a unique object that knows nothing about the others. Specializing or partial specializing a class is really just defining new types of a class. Without knowing the specific requirements you have all I can do is try and provide you examples that I think may aid your understanding but that are not necessarily the best solutions to specific problems (does that make sense?).
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 24835656
Yes, that's perfect! :) No specific requirements just remaining questions on templates. ;) Cheers for the help, hope you enjoyed your evening! :)

Uni.
0
 
LVL 3

Author Closing Comment

by:Unimatrix_001
ID: 31602584
Excellent.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24835657
>> Cheers for the help, hope you enjoyed your evening! :)
Muahahahaha.... I've just drunk lots of beer, not sure I should even be attempting template questions!

In fact, I am off to bed to sleep myself into a hangover ;)
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 24835662
Hahahaha! Enjoy your headache-to-come... ;)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question