Turning off template parameter deduction

Discussion in 'C++' started by KD, Jun 19, 2008.

  1. KD

    KD Guest

    I have a template function and I'm looking for a way to force the
    caller to specify the template parameters. In other words, I would
    like to turn off template parameter deduction. For example,

    template <class T>
    void foo(T t)
    {
    ...
    }

    foo(123); // I want this to fail.
    foo<int>(123); // I want this to succeed.

    I was hoping that the explicit keyword would work, but it didn't. So
    far, the only half working hack that I came up with is having two
    parameters S and T, where S is not used so it must be specified. In
    the method, I then do static checks that T is convertible to S, but it
    doesn't work in all cases and it adds some complexity to the code.

    Any ideas?
     
    KD, Jun 19, 2008
    #1
    1. Advertising

  2. KD

    Fei Liu Guest

    KD wrote:
    > I have a template function and I'm looking for a way to force the
    > caller to specify the template parameters. In other words, I would
    > like to turn off template parameter deduction. For example,
    >
    > template <class T>
    > void foo(T t)
    > {
    > ...
    > }
    >
    > foo(123); // I want this to fail.
    > foo<int>(123); // I want this to succeed.
    >
    > I was hoping that the explicit keyword would work, but it didn't. So
    > far, the only half working hack that I came up with is having two
    > parameters S and T, where S is not used so it must be specified. In
    > the method, I then do static checks that T is convertible to S, but it
    > doesn't work in all cases and it adds some complexity to the code.
    >
    > Any ideas?

    the more important question to ask is: why would you want to do
    something like that?

    Although I believe there are ways to do what you wanted but I don't
    think there is a generic solution.

    F
     
    Fei Liu, Jun 19, 2008
    #2
    1. Advertising

  3. KD

    gpderetta Guest

    On Jun 19, 3:51 pm, Fei Liu <> wrote:
    > KD wrote:
    > > I have a template function and I'm looking for a way to force the
    > > caller to specify the template parameters.  In other words, I would
    > > like to turn off template parameter deduction.  For example,

    >
    > > template <class T>
    > > void foo(T t)
    > > {
    > >     ...
    > > }

    >
    > > foo(123);  // I want this to fail.
    > > foo<int>(123); // I want this to succeed.

    >
    > > I was hoping that the explicit keyword would work, but it didn't.  So
    > > far, the only half working hack that I came up with is having two
    > > parameters S and T, where S is not used so it must be specified.  In
    > > the method, I then do static checks that T is convertible to S, but it
    > > doesn't work in all cases and it adds some complexity to the code.

    >
    > > Any ideas?

    >
    > the more important question to ask is: why would you want to do
    > something like that?


    To implement something similar to standard C++ casts for example.

    >
    > Although I believe there are ways to do what you wanted but I don't
    > think there is a generic solution.
    >


    Sure that there is: just put T in a non deducible context:

    template<typename T> struct identity { typedef T type; };

    template<typename T>
    void do_not_deduce(typename identity<T>::type x);

    HTH,

    --
    gpd
     
    gpderetta, Jun 19, 2008
    #3
  4. KD

    KD Guest

    On Jun 19, 11:08 am, gpderetta <> wrote:
    > On Jun 19, 3:51 pm, Fei Liu <> wrote:
    >
    >
    >
    > > KD wrote:
    > > > I have a template function and I'm looking for a way to force the
    > > > caller to specify the template parameters. In other words, I would
    > > > like to turn off template parameter deduction. For example,

    >
    > > > template <class T>
    > > > void foo(T t)
    > > > {
    > > > ...
    > > > }

    >
    > > > foo(123); // I want this to fail.
    > > > foo<int>(123); // I want this to succeed.

    >
    > > > I was hoping that the explicit keyword would work, but it didn't. So
    > > > far, the only half working hack that I came up with is having two
    > > > parameters S and T, where S is not used so it must be specified. In
    > > > the method, I then do static checks that T is convertible to S, but it
    > > > doesn't work in all cases and it adds some complexity to the code.

    >
    > > > Any ideas?

    >
    > > the more important question to ask is: why would you want to do
    > > something like that?

    >
    > To implement something similar to standard C++ casts for example.
    >
    >
    >
    > > Although I believe there are ways to do what you wanted but I don't
    > > think there is a generic solution.

    >
    > Sure that there is: just put T in a non deducible context:
    >
    > template<typename T> struct identity { typedef T type; };
    >
    > template<typename T>
    > void do_not_deduce(typename identity<T>::type x);
    >
    > HTH,
    >
    > --
    > gpd


    Thank you. This seems to work for me, and is cleaner than my previous
    solution.
     
    KD, Jun 19, 2008
    #4
  5. KD

    Road.Tang Guest

    On Jun 19, 10:08 pm, gpderetta <> wrote:
    > On Jun 19, 3:51 pm, Fei Liu <> wrote:
    >
    >
    >
    > > KD wrote:
    > > > I have a template function and I'm looking for a way to force the
    > > > caller to specify the template parameters. In other words, I would
    > > > like to turn off template parameter deduction. For example,

    >
    > > > template <class T>
    > > > void foo(T t)
    > > > {
    > > > ...
    > > > }

    >
    > > > foo(123); // I want this to fail.
    > > > foo<int>(123); // I want this to succeed.

    >
    > > > I was hoping that the explicit keyword would work, but it didn't. So
    > > > far, the only half working hack that I came up with is having two
    > > > parameters S and T, where S is not used so it must be specified. In
    > > > the method, I then do static checks that T is convertible to S, but it
    > > > doesn't work in all cases and it adds some complexity to the code.

    >
    > > > Any ideas?

    >
    > > the more important question to ask is: why would you want to do
    > > something like that?

    >
    > To implement something similar to standard C++ casts for example.
    >


    IMO, to implement cast like things doesn't need the trick.

    template <typename T, typename S> T& user_cast(S& s);

    T is result type, can't be deduced yet.

    >
    >
    > > Although I believe there are ways to do what you wanted but I don't
    > > think there is a generic solution.

    >
    > Sure that there is: just put T in a non deducible context:
    >
    > template<typename T> struct identity { typedef T type; };
    >
    > template<typename T>
    > void do_not_deduce(typename identity<T>::type x);
    >


    aha , impressive trick.

    > HTH,
    >
    > --
    > gpd
     
    Road.Tang, Jun 20, 2008
    #5
    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. Neelesh

    Template parameter Deduction

    Neelesh, Nov 10, 2005, in forum: C++
    Replies:
    4
    Views:
    444
    John Carson
    Nov 10, 2005
  2. Fei Liu
    Replies:
    0
    Views:
    432
    Fei Liu
    Oct 25, 2007
  3. Fei Liu
    Replies:
    4
    Views:
    799
    Victor Bazarov
    Oct 26, 2007
  4. er
    Replies:
    5
    Views:
    1,342
  5. Replies:
    2
    Views:
    342
Loading...

Share This Page