Hi,
I don't think I can't do this directly with standard C++, I've tried all
the ways I could think of that make sense and then some. I was wondering
if someone had a genius idea how I could produce behavior, using
standard C++, akin to what I guess can be best called a "typedef
specializations".
I.e. this being the default:
template <typename _T> class Foo {
public:
typedef _T type;
};
but in some cases (say _T is int) I want type to be a typedef for double
instead of int.
I've found loads of info on typedef templates, but this isn't quite the
same issue.
Any thoughts?
Thanks,
Alan 5 2118
Alan Woodland escribió: Hi,
I don't think I can't do this directly with standard C++, I've tried all the ways I could think of that make sense and then some. I was wondering if someone had a genius idea how I could produce behavior, using standard C++, akin to what I guess can be best called a "typedef specializations".
I.e. this being the default:
template <typename _T> class Foo { public: typedef _T type; };
but in some cases (say _T is int) I want type to be a typedef for double instead of int.
You could add an extra template to be used by typedef, as in:
template <typename _T, typename TDef = _T> class Foo
{
public:
typedef TDef type;
};
With this you only specify the extra template when you want the typedef
to be other than _T:
Foo<int, double> foo;
And you can keep using it like always like this:
Foo<char> foo;
- Eduardo Grajeda
Alan Woodland wrote: Hi,
I don't think I can't do this directly with standard C++, I've tried all the ways I could think of that make sense and then some. I was wondering if someone had a genius idea how I could produce behavior, using standard C++, akin to what I guess can be best called a "typedef specializations".
I.e. this being the default:
template <typename _T> class Foo {
_T probably isn't a good idea.
public: typedef _T type; };
but in some cases (say _T is int) I want type to be a typedef for double instead of int.
I've found loads of info on typedef templates, but this isn't quite the same issue.
Any thoughts?
Which typedef are you referring to, the one inside the template?
something like
template <typename T, typename T1 = T> class Foo {
public:
typedef T1 type;
};
maybe?
--
Ian Collins.
Alan Woodland wrote: Hi,
I don't think I can't do this directly with standard C++, I've tried all the ways I could think of that make sense and then some. I was wondering if someone had a genius idea how I could produce behavior, using standard C++, akin to what I guess can be best called a "typedef specializations".
I.e. this being the default:
template <typename _T> class Foo { public: typedef _T type; };
but in some cases (say _T is int) I want type to be a typedef for double instead of int.
I've found loads of info on typedef templates, but this isn't quite the same issue.
Any thoughts?
Add the following to your header file:
template<> class Foo<int> {
public:
typedef double type;
};
This is called template specializiation if you want to google around.
Alan Woodland wrote: Hi,
I don't think I can't do this directly with standard C++, I've tried all the ways I could think of that make sense and then some. I was wondering if someone had a genius idea how I could produce behavior, using standard C++, akin to what I guess can be best called a "typedef specializations".
I.e. this being the default:
template <typename _T> class Foo { public: typedef _T type; };
but in some cases (say _T is int) I want type to be a typedef for double instead of int.
I've found loads of info on typedef templates, but this isn't quite the same issue.
Any thoughts?
First things first: don't use _T. All identifiers starting with an
underscore and a capital letter are reserved for any use by the
implementation. Just use T.
To answer your question: You can specialize the whole class for int:
template<> class Foo<int>
{
typedef double type;
};
Alternately, you could use some template trickery such as Loki::Select
and Loki::IsSameType (see http://sourceforge.net/projects/loki-lib/) or
Boost.TypeTraits in conjunction with Boost.MPL (see http://boost.org/libs/mpl/doc/refmanual/if.html and http://boost.org/doc/html/boost_type...traits.is_same
or http://boost.org/doc/html/boost_type...s.is_integral).
It would look something like:
template <typename T>
struct Foo
{
typedef typename Loki::Select
<
Loki::IsSameType<int,T>::value, double, T::Result type;
};
If you're going to be doing this for multiple types (e.g., int/double,
short/float, char/int), there may be some ways to simplify it using
typelists.
Cheers! --M
Markus Schoder escribió: Alan Woodland wrote: Hi,
I don't think I can't do this directly with standard C++, I've tried all the ways I could think of that make sense and then some. I was wondering if someone had a genius idea how I could produce behavior, using standard C++, akin to what I guess can be best called a "typedef specializations".
I.e. this being the default:
template <typename _T> class Foo { public: typedef _T type; };
but in some cases (say _T is int) I want type to be a typedef for double instead of int.
I've found loads of info on typedef templates, but this isn't quite the same issue.
Any thoughts?
Add the following to your header file:
template<> class Foo<int> { public: typedef double type; };
This is called template specializiation if you want to google around.
However, if your is more than just the typedef, things might get
complicated when specializing, consider this:
template <typename T> class Foo {
public:
typedef T type;
void shake() { }
void dance() { }
};
If you want your specialized class to be the same, you'll have to
rewrite the methods on the specialized class:
template <> class Foo<int> {
public:
typedef double type;
void shake() { }
void dance() { }
};
To avoid this derive Foo from a base class that takes care of the
typedef, like this:
template <typename T> struct Base { // struct to avoid public:
typedef T type;
};
template <> struct Base<int> {
typedef double type;
};
template <typename T> class Foo : public Base<T> {
public:
void shake() { }
void dance() { }
};
Repeating only the typedef is way less complicated than the entire
content of the class.
- Eduardo Grajeda This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Thomas Matthews |
last post by:
Hi,
I have a templated class field:
template <typename Value_Type>
class Field
{
public:
Value_Type value;
};
|
by: CoolPint |
last post by:
I would be grateful if someone could point out if I am understanding
correctly and suggest ways to improve. Sorry for the long message and
I hope you will kindly bear with it. I have to make it...
|
by: christopher diggins |
last post by:
I just wrote an article on using template specializations for event-driven
programming ( http://www.artima.com/weblogs/viewpost.jsp?thread=84958 ).
Are there any other examples of this kind of...
|
by: Imre |
last post by:
I'm planning to create a macro-based property system (reflection,
automatic serializing for properties, that kind of stuff). The system
would involve writing some PROPERTY(propName) macros between...
|
by: Imre |
last post by:
Let's suppose we have a primary template with one argument defined in a
header file. Two source files include this header, and both define a
specialization of the primary template. Later, both...
| |
by: Samee Zahur |
last post by:
Why aren't we allowed to do partial specializations like these on
numeric template parameters? That would have allowed us to do all
kinds of interesting stuffs like loops of variable nesting (bad...
|
by: Alex |
last post by:
Hello people,
I am getting errors from VS2003 when working with typedef'ed types.
For example, assume that I have a type T, defined in a 3rd party include file based on some condition
#if...
|
by: Martin |
last post by:
I have a couple of partial specializations of a templated class, but
my code results in "undefined reference" errors by the linker.
Can't a partial specialization reuse member functions of the...
|
by: Frank Bergemann |
last post by:
Hi,
the (gcc-3.4.4) compiler complains, if i try to use a typedef of
subclass in superclass:
|padsol15 141make Helper_test
Helper_test.cc
In file included from Helper_test.cc:17:...
|
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: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |