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