470,841 Members | 948 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,841 developers. It's quick & easy.

Is this template possible?

Is there anyway I can do something like this? This won't compile. Any
ideas.

#include <iostream>

template <typename T>
class sample
{
public:
sample(T n) { std::cout << sizeof(n) << "\n"; }
~sample();
};

int main(int argc, char *argv[])
{
unsigned char n = 0x55;

sample s(n);
return 0;
}

--
http://www.munted.org.uk

Take a nap, it saves lives.
Jun 1 '06 #1
8 1520
Alex Buell wrote:
Is there anyway I can do something like this? This won't compile. Any
ideas.

#include <iostream>

template <typename T>
class sample
{
public:
sample(T n) { std::cout << sizeof(n) << "\n"; }
~sample();
};

int main(int argc, char *argv[])
{
unsigned char n = 0x55;

sample s(n);
return 0;
}


#include <iostream>

class sample
{
public:

template < typename T >
sample(T const & n) {
std::cout << sizeof(T) << "\n";
}

};

int main(int argc, char *argv[])
{
unsigned char n = 0x55;
sample s(n);
return 0;
}
But *why*?
Best

Kai-Uwe Bux
Jun 1 '06 #2
Alex Buell wrote:
Is there anyway I can do something like this? This won't compile. Any
ideas.

template <typename T>
class sample
{
public:
sample(T n) { std::cout << sizeof(n) << "\n"; }
~sample();
};

int main(int argc, char *argv[])
{
unsigned char n = 0x55;
sample s(n);
return 0;
}


Hmm, so you want the compiler to infer the class template parameter
from the constructor? Seems you can't, and I guess it's because the
compiler must know which class template to instantiate before it can
look up the constructor. Yeah, I'm pretty sure that's it. But you can
templatize the constructor itself, as already mentioned.

Luke

Jun 1 '06 #3
Alex Buell wrote:
Is there anyway I can do something like this? This won't compile. Any
ideas.

#include <iostream>

template <typename T>
class sample
{
public:
sample(T n) { std::cout << sizeof(n) << "\n"; }
~sample();
};
not very practical, but you can use

template <typename T>
sample<T> maker( const T t ) { return sample<T>( t ); }
int main(int argc, char *argv[])
{
unsigned char n = 0x55;

sample s(n); maker(n); return 0;
}

--
Ian Collins.
Jun 1 '06 #4
Luke Meyers wrote:
Alex Buell wrote:
Is there anyway I can do something like this? This won't compile. Any
ideas.

template <typename T>
class sample
{
public:
sample(T n) { std::cout << sizeof(n) << "\n"; }
~sample();
};

int main(int argc, char *argv[])
{
unsigned char n = 0x55;
sample s(n);
return 0;
}


Hmm, so you want the compiler to infer the class template parameter
from the constructor? Seems you can't, and I guess it's because the
compiler must know which class template to instantiate before it can
look up the constructor. Yeah, I'm pretty sure that's it. But you can
templatize the constructor itself, as already mentioned.


Oh, just thought of another option:

// free factory function
template <class T>
sample<T> createSample(T const& t);

Luke

Jun 1 '06 #5
On Thu, 01 Jun 2006 05:28:49 -0400, I waved a wand and this message
magically appeared from Kai-Uwe Bux:
class sample
{
public:

template < typename T >
sample(T const & n) {
std::cout << sizeof(T) << "\n";
}

};


Ah, brilliant thanks.
--
http://www.munted.org.uk

Take a nap, it saves lives.
Jun 1 '06 #6
Ian Collins wrote:
Alex Buell wrote:
Is there anyway I can do something like this? This won't compile. Any
ideas.

#include <iostream>

template <typename T>
class sample
{
public:
sample(T n) { std::cout << sizeof(n) << "\n"; }
~sample();
};

not very practical, but you can use

template <typename T>
sample<T> maker( const T t ) { return sample<T>( t ); }


Why not practical? The C++ standard library does that, too. Think of
std::make_pair.
int main(int argc, char *argv[])
{
unsigned char n = 0x55;

sample s(n);

maker(n);
return 0;
}



Jun 1 '06 #7
Rolf Magnus wrote:
Ian Collins wrote:


not very practical, but you can use

template <typename T>
sample<T> maker( const T t ) { return sample<T>( t ); }

Why not practical? The C++ standard library does that, too. Think of
std::make_pair.

I'd overlooked return value optimisations, so I guess it is practical,
as long as you don't use it as a syntactic crutch for frequently
building complex objects.

This example and std::make_pair both create simple objects.

--
Ian Collins.
Jun 1 '06 #8
Alex Buell wrote:
Is there anyway I can do something like this? This won't compile. Any
ideas.

#include <iostream>

template <typename T>
class sample
{
public:
sample(T n) { std::cout << sizeof(n) << "\n"; }
~sample();
Your destructor is declared but not defined.
};

int main(int argc, char *argv[])
{
unsigned char n = 0x55;

sample s(n);
try...
sample<unsigned char> s(n);
return 0;
}


This works:

#include <iostream>
#include <ostream>

template< typename T >
class Sample
{
T t;
public:
Sample(T n) : t(n) { }
~Sample() { }
T get() const { return t; }
};

int main()
{
typedef unsigned char UChar;
UChar uc = 0x137;

Sample< UChar > sample(uc);
std::cout << sample.get() << std::endl;

return 0;
}

/*
7
*/

Jun 1 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Sebastian Faust | last post: by
4 posts views Thread by Marc Schellens | last post: by
7 posts views Thread by Thomas Matthews | last post: by
2 posts views Thread by Capstar | last post: by
4 posts views Thread by Arne Claus | last post: by
5 posts views Thread by Steve | last post: by
4 posts views Thread by Schüle Daniel | last post: by
45 posts views Thread by charles.lobo | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.