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. 8 1650
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
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
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.
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
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.
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; }
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.
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
*/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Sebastian Faust |
last post by:
Hi,
I have 4 questions related to templates.
I wanna do something like the following:
template<typename T>
class Template
{
public:
Template_Test<T>()
|
by: FrankS |
last post by:
Hi All,
I have a problem with an call-template cmd at xslt 1.0:
With: pCall = 'ExInput'
------
I try to:
<xsl:call-template name="{$pCall}">
<xsl:with-param name="pVal" select="$pValue"/>...
|
by: Marc Schellens |
last post by:
I posted a similar question some time ago, but didn't get an
satisfying answer.
Lets say I have a template and five integer and two floating
types.
template <typename T> class A
{
A() {}...
|
by: Thomas Matthews |
last post by:
Hi,
I am converting my table and record classes into templates.
My issue is the syntax of declaring a friend class within
the template. I have searched the C++ FAQ Lite (web),
the C++...
|
by: Capstar |
last post by:
Hi NG,
Is it possible to make a template class, which has only one method that
makes use of the template type. The rest of the methods will work on the
base class of which the template type...
|
by: Arne Claus |
last post by:
Hi
I got a headache on this problem, maybe someone can help me here
I want to create a class which can be handled like this
manager<int, list> // possible variant
manager<int, vector> //...
|
by: Steve |
last post by:
Hi,
Does C++ allow the programmer to declare a template with in a template
so that a generic function can instantiate the embedded template? For
example, could code such as this exist:
...
|
by: Schüle Daniel |
last post by:
Hello all,
my question is basically wheather it's possible to
have nested templates .. I mean the following
if there are one templ. class and one global templ. function
is it possible to...
|
by: charles.lobo |
last post by:
Hi,
I have recently begun using templates in C++ and have found it to be
quite useful. However, hearing stories of code bloat and assorted
problems I decided to write a couple of small programs...
|
by: year1943 |
last post by:
For
template <typename Tclass My ;
I can define partial spec-ns somewhat like
template <typename Tclass My<T*; or
template <typename Tclass My<Another<T ;
And full spec-n, say
template <class...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |