Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Type traits with non-template variadic function

Thread Tools

Type traits with non-template variadic function

Posts: n/a
I have some type traits to create different object based on
different type. I would use non-template overload function
to do it because it's easily to handle derived class.

Sample code:

SomeFoo * createFoo(...); // #1
SomeFoo * createFoo(MyClass *); // #2

#1 is a general trap-all function to catch all unhandled
types. #2 is a traits for MyClass.

The problem is, for a given type T, the function parameter
should be always T * rather than T or T &, because the
general "..." version can't handle non trivial T.

That means, if I want to make traits for MyClass, I have to write,
SomeFoo * createFoo(MyClass *);
If I want to make traits for MyClass *, I have to write,
SomeFoo * createFoo(MyClass **);

There is always an extra pointer in the parameter, which is
not good and quite confusing.

So my question is, is there any way to eliminate the extra pointer
if T is not always a pointer?

There are two alternative solutions for it I can think of, neither
is good enough:

1, Use a trap all class.
struct TrapAll { template<typename T> TrapAll(const T &) {} };
Change the general function to
SomeFoo * createFoo(TrapAll);
It doesn't work if the class T has an implicit constructor,
such as std::string, which will cause ambiguous.

2, Change the general function to template.
template <typename T>
SomeFoo * createFoo(const T &);
Then the derived class of MyClass will be handled by the template
rather than #2 traits, which is wrong.

Any suggestions?


Reply With Quote
Posts: n/a
For the alternative 2,
> 2, Change the general function to template.

It's possible to use some kind of enable_if and
is_derived_from type traits to make it work for
derived class.
However, I would avoid it because I want the end
user to create the traits, who may not know
how to use those type traits.

Reply With Quote

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
How to generate execute file that include enthought.traits.api ,enthought.traits.ui.api ? ray Python 1 06-04-2010 03:49 PM
Call again a variadic function (... variable number of arguments)with same arguments that its variadic wrapper C Programming 3 12-31-2008 07:13 AM
variadic function calling variadic function C Programming 5 05-03-2006 05:23 PM
Variadic functions calling variadic functions with the argument list, HLL bit shifts on LE processors Ross A. Finlayson C Programming 19 03-10-2005 03:57 AM
is casting a variadic function to a non-variadic one legal? Colin Walters C Programming 2 02-13-2004 10:55 PM