14
struct Bob
{
    template<class T>
    void operator () () const
    {
        T t;
    }

    template<class T>
    operator T () const
    {
        T t;
        return t;
    }
};

I can directly call Bob's operator() like this

Bob b;
b.operator()<int>();

How to directly call the conversion operator with a specific template parameter like this?

Bob b;
std::string s = b.???<std::string>();

It's not possible to use static_cast

Bob b;
std::string s = static_cast<std::string>(b);

http://ideone.com/FoBKp7

error: call of overloaded ‘basic_string(Bob&)’ is ambiguous

Question How to call directly with template parameter OR it's not possible. I know there are workarounds using a wrapping function.

Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
  • 1
    hah, nice try. No, not possible as far as I'm aware. that's why I suggested using a real function. Besides, what's the point of making it a conversion function if you're going to call it like a regular one? – Dave Aug 30 '13 at 18:19
  • @Dave The same for the other operators. A backup in case you want to for some reason. – Neil Kirk Aug 30 '13 at 18:20
  • 1
    And that, children, is why implicit conversions are evil. – Konrad Rudolph Aug 30 '13 at 18:21
  • 6
    What's wrong with `b.operator std::string()`? – gx_ Aug 30 '13 at 18:21
  • @gx_ Nothing, I assume. Write an answer.. – Konrad Rudolph Aug 30 '13 at 18:23
  • 1
    Or even `b.operator decltype(s)();` (which I was admittedly surprised worked in clang). Then again, so does simple `std::string s = b;` so I guess that makes sense. – WhozCraig Aug 30 '13 at 18:24
  • Well there you are. Now I've learned two things about cast functions today. (I'll likely never use either because they're ugly but hey, knowledge) – Dave Aug 30 '13 at 18:24

2 Answers2

20

You can call it directly (explicitly) like this:

Bob b;
std::string s = b.operator std::string();

but it's not "with a specific template parameter" (but there's no need for).

See also WhozCraig's comment

Community
  • 1
  • 1
gx_
  • 4,690
  • 24
  • 31
  • 1
    Fwiw, `std::string s = b;` also works (at least on my rig). But thats beginning to paddle away from the original question at a pretty rapid rate =) – WhozCraig Aug 30 '13 at 18:29
  • @WhozCraig Of course (the purpose of a conversion operator is to enable implicit conversion after all). But I linked to your other comment that adds `decltype(s)` :) – gx_ Aug 30 '13 at 18:31
3

Use a helper function:

template< typename T >
T explicit_cast( T t ) { return t; }

int main()
{
    Bob b;
    std::string s = explicit_cast<std::string>(b);
}
Daniel Frey
  • 55,810
  • 13
  • 122
  • 180
  • 4
    *cough*... value parameter ... *cough*. – WhozCraig Aug 30 '13 at 18:19
  • I want to know if it is possible or not to call directly. – Neil Kirk Aug 30 '13 at 18:19
  • @WhozCraig I think in this case a by-value parameter is better than a by-const-reference as it allows more optimization. Or do you think otherwise? – Daniel Frey Aug 30 '13 at 18:23
  • I think *both* value and reference should be supplied via overload, but then again, I'm not sure I'd ever find myself in a position to do *either* =) – WhozCraig Aug 30 '13 at 18:25
  • 1
    Your "explicit_cast" resembles [`implicit_cast`](http://stackoverflow.com/a/863761) (which is better defined) – gx_ Aug 30 '13 at 18:40