Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is a "convert to any type" operator overkill and dangerous? template<typename T> operator T ()

Reply
Thread Tools

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

 
 
Qi
Guest
Posts: n/a
 
      06-30-2011
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?


--
WQ
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-30-2011
On 6/30/2011 9:20 AM, Qi wrote:
> 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
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
Qi
Guest
Posts: n/a
 
      07-01-2011
On 2011-7-1 1:22, Victor Bazarov wrote:
>
> 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.


--
WQ
 
Reply With Quote
 
 
 
Reply

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
does this site overkill and suck? richard HTML 2 08-18-2008 09:43 PM
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
Security overkill? Louise Computer Security 24 07-16-2005 06:02 AM
Overkill Bren Computer Security 11 04-16-2005 04:22 AM
is JNI overkill SirThanxALot Java 4 01-25-2005 12:51 PM



Advertisments