Function template overload puzzle

Lingxi

I have a class and a class template

class promise;

template <class... Ts>
class typed_promise;

I intend to design a cast utility to cast between them

template <class... Ts>
typed_promise<Ts...> promise_cast(promise x);

template <class... Ts>
promise promise_cast(typed_promise<Ts...> x);

So, you can do the following cast

promise pms;
typed_promise<int, int> typed_pms;
auto typed_pms2 = promise_cast<int, int>(pms);
auto pms2 = promise_cast(typed_pms);

I also want to support usage like

auto typed_pms2 = promise_cast<typed_promise<int, int>>(pms);

which is equivalent to

auto typed_pms2 = promise_cast<int, int>(pms);

Since C++ does not allow partial function template specialization, is it possible to achieve what I want? If possible, how should I do it?

T.C.

Dispatch to a class template that you can partially specialize.

template<class... Ts>
struct promise_cast_impl {
    static typed_promise<Ts...> do_cast(promise) {
        // do stuff
    }
};

template<class... Ts>
struct promise_cast_impl<typed_promise<Ts...>> {
    static typed_promise<Ts...> do_cast(promise p){
        return promise_cast_impl<Ts...>::do_cast(std::move(p));
    }
};

template<class... Ts>
auto promise_cast(promise x) 
     -> decltype(promise_cast_impl<Ts...>::do_cast(std::move(x))){
    return promise_cast_impl<Ts...>::do_cast(std::move(x));
}

If you don't use Ts... anywhere else, you can simplify this slightly by writing a metafunction that just computes the return type from Ts....

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Template function overload resolution

From Dev

Function template overload resolution

From Dev

template function overload with const

From Dev

Template function overload for base class

From Dev

C++ function template overload on template parameter

From Dev

Passing template function and overload as function argument

From Dev

How to overload a template function to match specific containers?

From Dev

begin() and end() free function overload on template

From Dev

Function template overload resolution with a pointer argument

From Dev

Passing a reference to template function call operator overload

From Dev

How to overload a template function for use with enums?

From Dev

How do you overload a variadic template function?

From Dev

Passing a reference to template function call operator overload

From Dev

C++ template function overload resolution bug

From Dev

Template Function Overload Resolution with sfml vectors

From Dev

"Overload" function template based on function object operator() signature

From Dev

"Overload" function template based on function object operator() signature

From Dev

Template function without explicit usage of type, strange overload ranking

From Dev

Can't see function-template overload from recursive call

From Dev

C++ Overload Resolution, userdefined conversion and function template

From Dev

C++ template specialization: unexpected function overload lookup result

From Dev

Is a c++11 variadic function template overload with a dependent type ambiguous?

From Dev

how to overload operator == outside template class using friend function?

From Dev

function template overload resolution with user defined conversion operator

From Dev

C++ gcc function template overload compiler issue

From Dev

how to overload operator == outside template class using friend function?

From Dev

function template overload resolution with user defined conversion operator

From Dev

Mahattan Function For This Puzzle Game

From Dev

Python Function Decorator Puzzle

Related Related

  1. 1

    Template function overload resolution

  2. 2

    Function template overload resolution

  3. 3

    template function overload with const

  4. 4

    Template function overload for base class

  5. 5

    C++ function template overload on template parameter

  6. 6

    Passing template function and overload as function argument

  7. 7

    How to overload a template function to match specific containers?

  8. 8

    begin() and end() free function overload on template

  9. 9

    Function template overload resolution with a pointer argument

  10. 10

    Passing a reference to template function call operator overload

  11. 11

    How to overload a template function for use with enums?

  12. 12

    How do you overload a variadic template function?

  13. 13

    Passing a reference to template function call operator overload

  14. 14

    C++ template function overload resolution bug

  15. 15

    Template Function Overload Resolution with sfml vectors

  16. 16

    "Overload" function template based on function object operator() signature

  17. 17

    "Overload" function template based on function object operator() signature

  18. 18

    Template function without explicit usage of type, strange overload ranking

  19. 19

    Can't see function-template overload from recursive call

  20. 20

    C++ Overload Resolution, userdefined conversion and function template

  21. 21

    C++ template specialization: unexpected function overload lookup result

  22. 22

    Is a c++11 variadic function template overload with a dependent type ambiguous?

  23. 23

    how to overload operator == outside template class using friend function?

  24. 24

    function template overload resolution with user defined conversion operator

  25. 25

    C++ gcc function template overload compiler issue

  26. 26

    how to overload operator == outside template class using friend function?

  27. 27

    function template overload resolution with user defined conversion operator

  28. 28

    Mahattan Function For This Puzzle Game

  29. 29

    Python Function Decorator Puzzle

HotTag

Archive