generic class with numeric type?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hyun-jik Bae

    generic class with numeric type?

    Is that not allowed to assume generic type numeric type? As far as I've
    tried, I got an error with the following code:

    public class AAA<T>
    {
    public int Foo(T a)
    {
    return a; // error: Cannot implicitly convert type 'T' to 'int'
    }
    }

    public class BBB
    {
    public int Goo()
    {
    int x = 3;
    return AAA<int>.Foo(x) ;
    }
    }

    where my intention is to use AAA<on numeric value only, which is
    acceptable in C++.

    How can I resolve that compilation error? Please reply.
    Thanks in advance.

    Hyun-jik Bae


  • Carl Daniel [VC++ MVP]

    #2
    Re: generic class with numeric type?

    Hyun-jik Bae wrote:
    Is that not allowed to assume generic type numeric type? As far as
    I've tried, I got an error with the following code:
    >
    public class AAA<T>
    {
    public int Foo(T a)
    {
    return a; // error: Cannot implicitly convert type 'T' to
    'int' }
    }
    >
    public class BBB
    {
    public int Goo()
    {
    int x = 3;
    return AAA<int>.Foo(x) ;
    }
    }
    >
    where my intention is to use AAA<on numeric value only, which is
    acceptable in C++.
    >
    How can I resolve that compilation error? Please reply.
    Since .NET generics are fully compiled and represented in the IL, in order
    for that IL to be verifiable (provably safe), there are limitations on what
    ..NET generics can do - they are severely limited compared to C++ templates
    (and also have significant advantages compared to C++ templates in some
    cases).

    To use a type as a generic type parameter, when you write the generic class,
    you specify constraints on the generic type parameters. These constraints
    actually increase what you can do with the type. There are only two types
    of constraints: a constructor contraint, written as new(), and an interface
    constraint.

    Unfortunately, there's no "IConvertsToInt " interface defined by the .NET
    framework, and defining such an interface yourself would be unfulfilling at
    best.

    Your best bet in the example you supplied is probably to use the Convert
    class

    public class AAA<T>
    {
    public int Foo(T a)
    {
    return Convert.ToInt32 (a);
    }
    }

    public class BBB
    {
    public int Goo()
    {
    int x = 3;
    return AAA<int>.Foo(x) ;
    }
    }

    Unfortunately, this will result in 'a' being boxed onto the managed heap,
    since Convert.ToIn32 takes a parameter of type System.Object. But it will
    work.

    Doing arithmetic - even simple comparisions - with generic parameters is
    hard. There are a couple of artibles on code project that explain the
    limitations and what you can do to work around them.

    Here's one of them - there's at least one other good one, but I couldn't
    find it with a quick search.



    -cd



    Comment

    • Marc

      #3
      Re: generic class with numeric type?

      Hyun-jik Bae schrieb:
      Is that not allowed to assume generic type numeric type? As far as I've
      tried, I got an error with the following code:
      >
      public class AAA<T>
      {
      public int Foo(T a)
      {
      return a; // error: Cannot implicitly convert type 'T' to 'int'
      }
      }
      >
      public class BBB
      {
      public int Goo()
      {
      int x = 3;
      return AAA<int>.Foo(x) ;
      }
      }
      >
      where my intention is to use AAA<on numeric value only, which is
      acceptable in C++.
      >
      How can I resolve that compilation error? Please reply.
      Thanks in advance.
      >
      Hyun-jik Bae
      you can specify what type the generic type should be.

      public class AAA<T>
      where T: int
      {
      public int Foo(T a)
      {
      return Convert.ToInt32 (a);
      }
      }

      have a look to "where T ....". then you can do something like

      public T Foo(T a)
      {
      return a;
      }

      or

      public int Foo(T a)
      {
      return (int)a;
      }

      depends on what the method in praxis should do.
      hope this helps.

      cheers,
      marc

      Comment

      • Joanna Carter [TeamB]

        #4
        Re: generic class with numeric type?

        "Marc" <marc@idev.ch a écrit dans le message de news:
        1161587442.4399 50.277230@e3g20 00...legro ups.com...

        | you can specify what type the generic type should be.
        |
        | public class AAA<T>
        | where T: int
        | {
        | public int Foo(T a)
        | {
        | return Convert.ToInt32 (a);
        | }
        | }

        This is not possible, you really need to check suggestions before posting
        answers :-)

        Joanna

        --
        Joanna Carter [TeamB]
        Consultant Software Engineer


        Comment

        • Marc

          #5
          Re: generic class with numeric type?


          Joanna Carter [TeamB] schrieb:
          This is not possible, you really need to check suggestions before posting
          answers :-)
          yes, you are right.
          just wanna point to the "where" keyword for generics where one can
          limit the types that can be used for a generic. this could help here
          but it depends on the concrete implementation of the methods and their
          results. i mean, does it always need to be an int or just the same as
          the generic type?

          cheers,
          marc

          Comment

          Working...