Type traits with non-template variadic function

Q

Qi

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?
 
Q

Qi

PS:
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.
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top