Is this template possible?

A

Alex Buell

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;
}
 
K

Kai-Uwe Bux

Alex said:
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
 
L

Luke Meyers

Alex said:
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
 
I

Ian Collins

Alex said:
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 said:
int main(int argc, char *argv[])
{
unsigned char n = 0x55;

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

Luke Meyers

Luke said:
Alex said:
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
 
R

Rolf Magnus

Ian said:
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;
}
 
I

Ian Collins

Rolf said:
Ian Collins wrote:


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.
 
S

Salt_Peter

Alex said:
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 said:
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
*/
 

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,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top