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

Discussion in 'C++' started by Qi, Jun 30, 2011.

  1. Qi

    Qi Guest

    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
    Qi, Jun 30, 2011
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Jun 30, 2011
    #2
    1. Advertising

  3. Qi

    Qi Guest

    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
    Qi, Jul 1, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Denis Remezov
    Replies:
    1
    Views:
    2,132
    I wish
    Jun 25, 2004
  2. Replies:
    6
    Views:
    338
    Victor Bazarov
    May 10, 2005
  3. Jakob Bieling

    Q: typename or not typename?

    Jakob Bieling, Mar 14, 2006, in forum: C++
    Replies:
    2
    Views:
    354
    Rolf Magnus
    Mar 14, 2006
  4. Replies:
    10
    Views:
    920
    ManicQin
    May 24, 2008
  5. richard
    Replies:
    2
    Views:
    438
    John Hosking
    Aug 18, 2008
Loading...

Share This Page