Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is this template possible?

Reply
Thread Tools

Is this template possible?

 
 
Alex Buell
Guest
Posts: n/a
 
      06-01-2006
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.
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      06-01-2006
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
 
Reply With Quote
 
 
 
 
Luke Meyers
Guest
Posts: n/a
 
      06-01-2006
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

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      06-01-2006
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.
 
Reply With Quote
 
Luke Meyers
Guest
Posts: n/a
 
      06-01-2006
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

 
Reply With Quote
 
Alex Buell
Guest
Posts: n/a
 
      06-01-2006
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.
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      06-01-2006
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;
>> }
>>

>
>


 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      06-01-2006
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.
 
Reply With Quote
 
Salt_Peter
Guest
Posts: n/a
 
      06-01-2006
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
*/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
r H2 deduce deduce template argument of a template class inheritingfrom a non template base? nguillot C++ 5 03-08-2009 05:56 PM
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
template template arguments: expected a class template, got `Component<T1, T2, T3> gary.bernstein@gmail.com C++ 1 06-08-2007 07:10 AM
Re: A Newbie Question about template template template tom_usenet C++ 0 07-24-2003 12:06 PM
Re: A Newbie Question about template template template Chris Theis C++ 2 07-24-2003 09:42 AM



Advertisments