partial specialization of function template

Discussion in 'C++' started by Alexander Stippler, Aug 15, 2003.

  1. Hi,

    the following code does not compile. Why and how can I reach the desired
    effect?

    template <int n, typename T>
    double
    norm(const T &t)
    { return 0.0; }

    template <typename T>
    double
    norm<2,T>(const T &)
    { return 2.0; }

    class A
    {
    };

    int
    main()
    {
    A a;
    norm<2>(a);

    return 0;
    }

    regards,
    alex
    Alexander Stippler, Aug 15, 2003
    #1
    1. Advertising

  2. Alexander Stippler wrote in news:-ulm.de:

    > Hi,
    >
    > the following code does not compile. Why and how can I reach the
    > desired effect?
    >
    > template <int n, typename T>
    > double
    > norm(const T &t)
    > { return 0.0; }
    >
    > template <typename T>
    > double
    > norm<2,T>(const T &)
    > { return 2.0; }
    >
    > class A
    > {
    > };
    >
    > int
    > main()
    > {
    > A a;
    > norm<2>(a);
    >
    > return 0;
    > }
    >


    #include <iostream>
    #include <ostream>

    template < int N, typename T>
    struct do_norm
    {
    static double apply( T const &t )
    {
    return 0.0;
    }
    };

    template < typename T >
    struct do_norm< 2, T >
    {
    static double apply( T const &t )
    {
    return 2.0;
    }
    };

    template < int N, typename T>
    double norm( T const &t )
    {
    return do_norm< N, T >::apply( t );
    }


    struct A {};

    int main()
    {
    A a;
    std::cout << norm<2>(a) << std::endl;
    std::cout << norm<1>(1) << std::endl;
    }

    HTH

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Aug 15, 2003
    #2
    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. mrstephengross
    Replies:
    1
    Views:
    595
    Victor Bazarov
    Apr 14, 2005
  2. Levent
    Replies:
    5
    Views:
    11,144
    Victor Bazarov
    May 10, 2005
  3. toton
    Replies:
    1
    Views:
    612
  4. vj
    Replies:
    1
    Views:
    483
  5. Hizo
    Replies:
    17
    Views:
    684
    itaj sherman
    Mar 7, 2011
Loading...

Share This Page