Template constructors of template classes

D

Daniel Pitts

I have a template:
template<typename t, int foo>
class MyTemplate {
public:
template<typename bar>
MyTemplate() {
bar::something(foo);
}
}

How would I instantiate a local instance of this class?

MyTemplate<SomeType, 3> <SomeBarType>() doesn't seem to work.
Nor does any other things I've tried.
 
D

Daniel Pitts

Victor said:
Victor said:
Daniel said:
I have a template:
template<typename t, int foo>
class MyTemplate {
public:
template<typename bar>
MyTemplate() {
bar::something(foo);
}
}

How would I instantiate a local instance of this class?

MyTemplate<SomeType, 3> <SomeBarType>() doesn't seem to work.

Are you trying to create a temporary of that type?
Nor does any other things I've tried.

You can't. A template[d] constructor has to have an argument from
which the compiler will deduce the template argument. In your case
you probably want to create a "factory method":

template<class bar> MyTemplate from() {

Should probably be

template said:
MyTemplate retval;
bar::something(foo);
return retval;
}

BTW, how would you use it?

V

V
I basically am trying to create a return value. I'm trying to create a
generic way to apply a binary operation to the components of two
Vectors, and return a third Vector with the results.
If you've seen my other recent posts, you might gather that Vector is a
creation of my own. I'm trying to do it in such a way that everything
that could be written to be inlined for specific sized vectors could
also be inlined in this generic sized vector.

For instance, I'm hoping that Vector<double, 2> and Vector<double, 15>
both have inlinable "operator+" calls.

I know this is premature optimization, but its more for practicing my
meta-programming skills :)
 
D

Daniel Pitts

Victor said:
Daniel said:
[..]
I basically am trying to create a return value. I'm trying to create a
generic way to apply a binary operation to the components of two
Vectors, and return a third Vector with the results.

Uh... I'm not following. Generic way? Like,

Vector<double,2> result = op<binary>(v1, v2);

, where 'binary' is actually unknown at the time of writing (a template
argument)?
More like
Vector said:
Design of templates grows from their use. If you just talk about how
you'd use your templates, you'll really never design them well. You
need to write code that would use those classes. From the use you will
see the necessary interfaces your templates need to provide. Basically
your code should become a set of unit tests for your template classes
and functions.

V
Yes, that is my general philosophy on designing software. I'm not
actually designing software here, I'm exploring and testing the language.
 
Joined
May 22, 2008
Messages
1
Reaction score
0
Hi,

This is not exactly what you asked, but you could use the fact that template functions can sometimes deduce the templates types upon inspection of the arguments, to make a parameterised constructor in the following way :

Code:
#include <iostream>
#include<list>
using namespace std;

struct cat{
	void something(int foo){
		cout<<"foo = "<<foo<<endl;
	}
};

template<typename t, int foo>
class mytemplate {
public:
	template<typename bar>
	mytemplate(bar a) {
		a.something(foo);
	}
};

int main(void){
	cat a;
	mytemplate <int,100> test(a);
	cin.get();
}

Then the implicit copy constructor is used when you apply the copy constructor on objects of the same type :

Code:
mytemplate <int,100> test2(test);
 
Last edited:

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
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top