Template parameters for template function

Discussion in 'C++' started by rush.william@gmail.com, Apr 12, 2007.

  1. Guest

    Hello All!

    There is following code:

    template < class T >
    void f(T const & value)
    {
    };

    Is there any way to force user to specify template parameters
    explicitly? I want to do the following:
    f<int>(1); // Normal
    f(1); // This should give an error

    Any ideas?...

    WBR
    , Apr 12, 2007
    #1
    1. Advertising

  2. wrote:
    > Hello All!
    >
    > There is following code:
    >
    > template < class T >
    > void f(T const & value)
    > {
    > };
    >
    > Is there any way to force user to specify template parameters
    > explicitly? I want to do the following:
    > f<int>(1); // Normal
    > f(1); // This should give an error
    >
    > Any ideas?...



    Try this:


    template < class T >
    struct f_helper
    {
    typedef T type;
    };


    template < class T >
    void f(typename f_helper<T>::type const & value)
    {
    };

    The compiler is unable to deduce the type T from f_helper<T>::type.
    Gianni Mariani, Apr 12, 2007
    #2
    1. Advertising

  3. Markus Moll Guest

    Hi

    wrote:
    > Is there any way to force user to specify template parameters
    > explicitly? I want to do the following:
    > f<int>(1); // Normal
    > f(1); // This should give an error
    >
    > Any ideas?...


    You would have to make T undeducible. (word sounds wrong?)
    You could for example use an indirection:

    template<typename T>
    struct f_helper
    {
    const T& value;
    f_helper(const T& value) : value(value) {}
    operator const T& () const { return value; }
    };

    template<typename T>
    void f(const f_helper<T>& h)
    {
    const T& value = h;
    // ...
    }

    Then f(1) fails to compile, while f<int>(1) will still compile.

    Markus
    Markus Moll, Apr 12, 2007
    #3
  4. Gianni Mariani:
    > wrote:
    > > Hello All!
    > >
    > > There is following code:
    > >
    > > template < class T >
    > > void f(T const & value)
    > > {
    > > };
    > >
    > > Is there any way to force user to specify template parameters
    > > explicitly? I want to do the following:
    > > f<int>(1); // Normal
    > > f(1); // This should give an error
    > >
    > > Any ideas?...

    >
    >
    > Try this:
    >
    >
    > template < class T >
    > struct f_helper
    > {
    > typedef T type;
    > };
    >
    >
    > template < class T >
    > void f(typename f_helper<T>::type const & value)
    > {
    > };
    >
    > The compiler is unable to deduce the type T from f_helper<T>::type.


    Thanks, it works!...
    Gleb Golubitsky, Apr 12, 2007
    #4
  5. Pete Becker Guest

    Markus Moll wrote:
    >
    > You would have to make T undeducible. (word sounds wrong?)
    >


    The standard doesn't use the word "deducible," so there's no direct help
    there. But it does use the word "deduced" as in "deduced context", and
    it uses the word "non-deduced" as its opposite.

    --

    -- Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com)
    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." (www.petebecker.com/tr1book)
    Pete Becker, Apr 12, 2007
    #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. ian
    Replies:
    5
    Views:
    516
  2. Sebastian Faust
    Replies:
    4
    Views:
    405
    Sebastian Faust
    Oct 18, 2003
  3. Jason
    Replies:
    2
    Views:
    494
    Jonathan Mcdougall
    May 13, 2006
  4. Peng Yu
    Replies:
    3
    Views:
    756
    Thomas J. Gritzan
    Oct 26, 2008
  5. kito
    Replies:
    2
    Views:
    406
Loading...

Share This Page