Is a "convert to any type" operator overkill and dangerous? template<typename T> operator T ()

Q

Qi

class SomeData
{
public:
template <typename T>
operator T () const {
// convert the object to T and return as T
}
};

I have a class to hold any data type (int, string, etc),
something like Windows COM VARIANT, to convert the object to real
data, I have two choices,

Choice 1, as above, write a type cast operator to convert
to any type.
Pros: simply use. The user may not notice the difference between
SomeData and other data type.
Cons: This is my question. Is it dangerous? I'm scared to see
an object can be implicitly converted to other data type.

I also guess it's quite error prone and hard to debug, am I right?

Is it common in C++ practice to make that kind of type cast
operator?

Choice 2, use a global template function to do that,
template <typename T>
T convert(const SomeData & data);

Pros: no implicitly type conversion.
Cons: tedious to call that function every where.
However, the syntax calling that function is quite like the built-in
cast operators, such like static_cast.

Final question, which choice is better you think?
 
V

Victor Bazarov

class SomeData
{
public:
template <typename T>
operator T () const {
// convert the object to T and return as T
}
};

I have a class to hold any data type (int, string, etc),
something like Windows COM VARIANT, to convert the object to real
data, I have two choices,

Choice 1, as above, write a type cast operator to convert
to any type.
Pros: simply use. The user may not notice the difference between
SomeData and other data type.
Cons: This is my question. Is it dangerous? I'm scared to see
an object can be implicitly converted to other data type.

I also guess it's quite error prone and hard to debug, am I right?

Is it common in C++ practice to make that kind of type cast
operator?

Choice 2, use a global template function to do that,
template <typename T>
T convert(const SomeData & data);

Pros: no implicitly type conversion.
Cons: tedious to call that function every where.
However, the syntax calling that function is quite like the built-in
cast operators, such like static_cast.

Final question, which choice is better you think?

I would vote for the latter approach. It's more visible in the code.
Imagine you have functions

void foo(std::string);
void foo(int);

and your type has a conversion operator for 'int' but not 'string'.
When you see

foo(object_of_your_type);

do you know which foo is called? You have to remember that it's 'int'
because 'object_of_your_type' cannot be converted to 'std::string'...
That's too much work. What I'd rather see is:

foo(object_of_your_type.as<int>());

('as' is the name of your template conversion function, for instance).

V
 
Q

Qi

do you know which foo is called? You have to remember that it's 'int'
because 'object_of_your_type' cannot be converted to 'std::string'...

That's a good point.
And I already removed the T() conversion from my code.

I think I should add another rule to my C++ mind:
Prefer explicit type conversion to implicit type conversion.
 

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

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top