How do you typdef a function template

Discussion in 'C++' started by Mark Snelling, Oct 24, 2005.

  1. I wish to typedef a templated function for readability but cannot find
    the correct syntax to do so. For example:

    #include <iostream>

    template< int I >
    void foo()
    {
    std::cout << I << std::endl;

    }

    typedef foo< 2 > foo2; // <--- I wish to do something like this

    int main( int argc, char* argv[] )
    {
    foo< 1 >();
    foo2();

    return 0;
    }

    In the above code I want to create a typedef for the function template
    foo() using the value 2 as the template parameter. Is this even
    possible?

    Cheers,

    Mark.
     
    Mark Snelling, Oct 24, 2005
    #1
    1. Advertising

  2. Mark  Snelling

    John Carson Guest

    "Mark Snelling" <> wrote in message
    news:
    > I wish to typedef a templated function for readability but cannot find
    > the correct syntax to do so. For example:
    >
    > #include <iostream>
    >
    > template< int I >
    > void foo()
    > {
    > std::cout << I << std::endl;
    >
    > }
    >
    > typedef foo< 2 > foo2; // <--- I wish to do something like this
    >
    > int main( int argc, char* argv[] )
    > {
    > foo< 1 >();
    > foo2();
    >
    > return 0;
    > }
    >
    > In the above code I want to create a typedef for the function template
    > foo() using the value 2 as the template parameter. Is this even
    > possible?


    You can't typedef regular functions, so why should you be able to typedef
    template functions? A function is not a type. A function pointer is a type,
    but that is a different matter.

    You could try

    inline void foo2()
    {
    foo<2>();
    }

    --
    John Carson
     
    John Carson, Oct 24, 2005
    #2
    1. Advertising

  3. Mark Snelling wrote:
    > I wish to typedef a templated function for readability but cannot find
    > the correct syntax to do so. For example:
    >
    > #include <iostream>
    >
    > template< int I >
    > void foo()
    > {
    > std::cout << I << std::endl;
    >
    > }
    >
    > typedef foo< 2 > foo2; // <--- I wish to do something like this
    >
    > int main( int argc, char* argv[] )
    > {
    > foo< 1 >();
    > foo2();
    >
    > return 0;
    > }
    >
    > In the above code I want to create a typedef for the function template
    > foo() using the value 2 as the template parameter. Is this even
    > possible?


    No. foo<2> is a function, not a type. You may do

    void foo2()
    {
    foo<2>();
    }


    Jonathan
     
    Jonathan Mcdougall, Oct 24, 2005
    #3
  4. Thanks, I didn't realise you couldn't typedef a function.
     
    Mark Snelling, Oct 24, 2005
    #4
  5. Mark  Snelling

    Greg Guest

    Mark Snelling wrote:
    > I wish to typedef a templated function for readability but cannot find
    > the correct syntax to do so. For example:
    >
    > #include <iostream>
    >
    > template< int I >
    > void foo()
    > {
    > std::cout << I << std::endl;
    >
    > }
    >
    > typedef foo< 2 > foo2; // <--- I wish to do something like this
    >
    > int main( int argc, char* argv[] )
    > {
    > foo< 1 >();
    > foo2();
    >
    > return 0;
    > }
    >
    > In the above code I want to create a typedef for the function template
    > foo() using the value 2 as the template parameter. Is this even
    > possible?


    No, it is not possible, nor does the attempt make much sense. Why
    declare a function template in order to use the number 2? The function
    is free to use the value 2 (or any other constant value) whenever and
    however it wants.

    Greg
     
    Greg, Oct 24, 2005
    #5
  6. Mark  Snelling

    Kai-Uwe Bux Guest

    Mark Snelling wrote:

    > I wish to typedef a templated function for readability but cannot find
    > the correct syntax to do so. For example:
    >
    > #include <iostream>
    >
    > template< int I >
    > void foo()
    > {
    > std::cout << I << std::endl;
    >
    > }
    >
    > typedef foo< 2 > foo2; // <--- I wish to do something like this
    >
    > int main( int argc, char* argv[] )
    > {
    > foo< 1 >();
    > foo2();
    >
    > return 0;
    > }
    >
    > In the above code I want to create a typedef for the function template
    > foo() using the value 2 as the template parameter. Is this even
    > possible?


    foo<2> is not a type. It is a constant of type void function of void. Thus
    you need a global constant:

    #include <iostream>

    template< int I >
    void foo()
    {
    std::cout << I << std::endl;

    }

    void (* const foo2)( void ) = foo<2>;

    int main ( void ) {
    foo2();
    }


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Oct 24, 2005
    #6
  7. Thanks for stating that what I wanted to do is not possible. However,
    saying that 'the attempt doesn't make sense' assumes knowledge about
    the actual problem on which the supplied trivial was based.
     
    Mark Snelling, Oct 24, 2005
    #7
  8. "Mark Snelling" <> wrote in message
    news:...
    > Thanks for stating that what I wanted to do is not possible. However,
    > saying that 'the attempt doesn't make sense' assumes knowledge about
    > the actual problem on which the supplied trivial was based.
    >


    Don't worry about him... he's just being an arrogant ass.
     
    Jon Slaughter, Oct 24, 2005
    #8
  9. Mark  Snelling

    Greg Guest

    Mark Snelling wrote:
    > Thanks for stating that what I wanted to do is not possible. However,
    > saying that 'the attempt doesn't make sense' assumes knowledge about
    > the actual problem on which the supplied trivial was based.


    The concept of a non-type function template makes little sense since
    there is nothing that meaningfully distinguishes such a function from
    an ordinary function that would make declaring one worthwhile.

    Greg
     
    Greg, Oct 24, 2005
    #9
  10. If it makes little sense, why is it included as part of the standard?

    So I can do things like this...

    template< int I >
    void add( int& a )
    {
    a += I;
    }

    int main( int argc, char* argv[] )
    {
    std::list< int > myList;
    myList.push_back( 1 );
    myList.push_back( 2 );
    myList.push_back( 3 );
    myList.push_back( 4 );

    std::for_each( myList.begin(), myList.end(), add< 2 > );
    std::for_each( myList.begin(), myList.end(), add< 200 > );

    return 0;
    }


    Again, a trivial example but proves my point.
     
    Mark Snelling, Oct 24, 2005
    #10
  11. Mark  Snelling

    Ron Natalie Guest

    Mark Snelling wrote:
    > If it makes little sense, why is it included as part of the standard?
    >

    It's not, that's the whole point.

    > So I can do things like this...
    >


    >
    > std::for_each( myList.begin(), myList.end(), add< 2 > );


    Since when is the third argument of std::for_each a type either?
    You seem to have a fundamental problem distinguishing between types
    and objects. Define a template function object and use that:

    template <int I> struct add {
    void operator()(int &a) {
    a += I;
    }
    };

    std::for_each(mylist.begin(), mylist.end(), add<2>());

    Actually you don't even need a template really.
    struct add {
    int I;
    add(int i) : I(i) { }
    void operator() (int& a) {
    a += i;
    }
    };

    std::for_each(mylist.begin(), mylist.end(), add(2));
     
    Ron Natalie, Oct 25, 2005
    #11
  12. Ron, actually I understand the difference between types and object very
    well. Are you saying that the example I supplied in my previous message
    is invalid? Compile it and see. I think you'll find that I'm right. It
    is valid code. It seems to me that you don't fully understand how you
    can use the STL algorithms.

    Everyone keeps getting hung up on the 'trivial' examples I'm posting.
    Just because you can see a 'better' way to do what my example is doing,
    doesn't mean that it fits the particular problem that I'm trying to
    solve. I am well aware of using function objects, or functors, but in
    my particular case they are inappropriate.
     
    Mark Snelling, Oct 25, 2005
    #12
  13. Mark  Snelling

    Kai-Uwe Bux Guest

    Mark Snelling wrote:

    > Ron, actually I understand the difference between types and object very
    > well. Are you saying that the example I supplied in my previous message
    > is invalid? Compile it and see. I think you'll find that I'm right. It
    > is valid code. It seems to me that you don't fully understand how you
    > can use the STL algorithms.
    >
    > Everyone keeps getting hung up on the 'trivial' examples I'm posting.
    > Just because you can see a 'better' way to do what my example is doing,
    > doesn't mean that it fits the particular problem that I'm trying to
    > solve. I am well aware of using function objects, or functors, but in
    > my particular case they are inappropriate.


    As long as the "'trivial' examples" you post do not parallel your problem,
    you should not be surprised that the solutions proposed by the posters who
    take the time of thinking through what you actually posted do not solve the
    "particular case" that you keep hiding from us.

    People in this group can only read what you post. Most fellows in this group
    do not engage in mind reading. Post the real problem, and you might have a
    shot at obtaining meaningful advice.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Oct 25, 2005
    #13
  14. I'm sure you understand that posting real problems with real code is a
    violation of most companies intellectual property policies, hence my
    posting of cut down examples. It is also considered bad form to post
    too much code when posting in these groups.

    I've actually had very helpful responses that have solved my problem
    from a lot of the people here as posts to this group and private
    emails. It's a shame though that there are some people that think there
    is only one way to skin a cat and others that simply make arrogant
    comments without providing any real assistance.
     
    Mark Snelling, Oct 25, 2005
    #14
  15. Mark  Snelling

    Kai-Uwe Bux Guest

    Mark Snelling wrote:

    > I'm sure you understand that posting real problems with real code is a
    > violation of most companies intellectual property policies, hence my
    > posting of cut down examples.


    If you refer to something, please quote it. I said:

    > As long as the "'trivial' examples" you post do not parallel your problem,
    > you should not be surprised that the solutions proposed by the posters who
    > take the time of thinking through what you actually posted do not solve
    > the "particular case" that you keep hiding from us.


    I was not suggesting to post "real code" but code that "parallels your
    problem". I am sure you understand the difference.


    > It is also considered bad form to post too much code when posting in
    > these groups.


    But it is necessary to post enough code to make your problem clear. And that
    is not frowned upon.


    > I've actually had very helpful responses that have solved my problem
    > from a lot of the people here as posts to this group and private
    > emails. It's a shame though that there are some people that think there
    > is only one way to skin a cat and others that simply make arrogant
    > comments without providing any real assistance.


    In case you find my comments arrogant, I apologize for hurting your
    feelings.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Oct 25, 2005
    #15
    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. Mark Piffer

    typdef'ing from sig_atomic_t valid?

    Mark Piffer, Apr 22, 2004, in forum: C Programming
    Replies:
    21
    Views:
    1,926
    Dan Pop
    Apr 29, 2004
  2. baumann@pan
    Replies:
    42
    Views:
    1,090
  3. Johan Tibell

    Typdef pointers to structs or not?

    Johan Tibell, Jul 27, 2006, in forum: C Programming
    Replies:
    17
    Views:
    561
  4. Dave
    Replies:
    1
    Views:
    329
    Ron Natalie
    Nov 9, 2005
  5. Christopher

    typdef deprecated?

    Christopher, Mar 9, 2009, in forum: C++
    Replies:
    5
    Views:
    406
    Triple-DES
    Mar 9, 2009
Loading...

Share This Page