Template where I need to know the type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jacek Dziedzic

    Template where I need to know the type

    Hello!

    In my code I have functions like

    SendToNode(int dest_node; int i);
    SendToNode(int dest_node; double d);
    SendToNode(int dest_node; unsigned int u);

    and so on, where the function body is the same for all
    these overloaded functions, except for one line, which
    depends on the type of the second argument to SendToNode,
    i.e. this line looks (more or less) like

    internal_send_f unction(dest_no de, type_is_int);
    internal_send_f unction(dest_no de, type_is_double) ;
    internal_send_f unction(dest_no de, type_is_unsigne d_int);

    where "type_is_in t" etc. are symbolic constants of type int.

    The situation is I'm not much of an OOP programmer, still
    it looks to me like my SendToNode function is a candidate for
    a template. The problem is: how do I do the "branching" in the
    template, depending on what the *type* of the argument is?
    How do I determine the type of that argument? I tried googling
    it up and found something called typeof(), but it looked as if
    it was not a part of the standard (perhaps "yet").

    Is this where the concept of traits comes into play? What's
    the story behind this?

    TIA,
    - J.


  • NFish

    #2
    Re: Template where I need to know the type

    Jacek Dziedzic wrote:
    [color=blue]
    > Hello!
    >
    > In my code I have functions like
    >
    > SendToNode(int dest_node; int i);
    > SendToNode(int dest_node; double d);
    > SendToNode(int dest_node; unsigned int u);
    >
    > and so on, where the function body is the same for all
    > these overloaded functions, except for one line, which
    > depends on the type of the second argument to SendToNode,
    > i.e. this line looks (more or less) like
    >
    > internal_send_f unction(dest_no de, type_is_int);
    > internal_send_f unction(dest_no de, type_is_double) ;
    > internal_send_f unction(dest_no de, type_is_unsigne d_int);
    >
    > where "type_is_in t" etc. are symbolic constants of type int.
    >
    > The situation is I'm not much of an OOP programmer,[/color]

    Lucky for you, templates don't have much to do with OOD.
    [color=blue]
    > still
    > it looks to me like my SendToNode function is a candidate for
    > a template. The problem is: how do I do the "branching" in the
    > template, depending on what the *type* of the argument is?[/color]

    It's called partial specialization, I think. But it won't help you much
    here.
    [color=blue]
    > How do I determine the type of that argument? I tried googling
    > it up and found something called typeof(),[/color]

    typeof() is a gcc extension, I think.
    [color=blue]
    > but it looked as if
    > it was not a part of the standard (perhaps "yet").
    >
    > Is this where the concept of traits comes into play? What's
    > the story behind this?
    >[/color]

    This seems pretty straightforward to me. Refactor your code into a
    function that does the shared stuff, and then add the one line that
    differs to each of the specialized functions. It appears that all you
    want to do is pass an int that depends on the static type, so you can
    use overloading.

    enum eType { eUnknown, eInt, eUnsignedInt, eDouble };

    void internal_send_f unction(int dest_node, enum eType type) {
    /* stuff goes here */
    }

    void SendToNode(int dest_node, int i) {
    internal_send_f unction(dest_no de, eInt);
    }

    void SendToNode(int dest_node, unsigned int i) {
    internal_send_f unction(dest_no de, eUnsignedInt);
    }

    void SendToNode(int dest_node, double i) {
    internal_send_f unction(dest_no de, eDouble);
    }

    It's not clear from the code you showed, but it looks like you aren't
    using the value of the passed in variable at all, which makes me
    question what exactly you're doing.

    -Peter

    Comment

    • Jacek Dziedzic

      #3
      Re: Template where I need to know the type

      "NFish" <nobody@nowhere .net> wrote in message
      news:6yWob.1915 $5A5.1261@newss vr27.news.prodi gy.com...[color=blue]
      > [...]
      > This seems pretty straightforward to me. Refactor your code
      > into a function that does the shared stuff, and then add the one
      > line that
      > differs to each of the specialized functions. It appears that all you
      > want to do is pass an int that depends on the static type, so you
      > can
      > use overloading.
      >
      > enum eType { eUnknown, eInt, eUnsignedInt, eDouble };
      >
      > void internal_send_f unction(int dest_node, enum eType type) {
      > /* stuff goes here */
      > }
      >
      > void SendToNode(int dest_node, int i) {
      > internal_send_f unction(dest_no de, eInt);
      > }
      >
      > void SendToNode(int dest_node, unsigned int i) {
      > internal_send_f unction(dest_no de, eUnsignedInt);
      > }
      >
      > void SendToNode(int dest_node, double i) {
      > internal_send_f unction(dest_no de, eDouble);
      > }[/color]

      Yes, but that still leaves me with a one-function-needed-
      per-every-type mess. Even though these functions are now
      reduced to one line, I thought that by some "template magic"
      I could stuff all that together into one function that would
      perhaps have some branches depending on the type of
      the argument, but not individual extra functions for every
      possible type that I'd use.
      [color=blue]
      > It's not clear from the code you showed, but it looks like you
      > aren't using the value of the passed in variable at all, which
      > makes me question what exactly you're doing.[/color]

      Ah yes, that was only a simplification, but I indeed forgot
      to include the variable itself in the argument list of
      SendToNode...

      - J.


      Comment

      • Howard Hinnant

        #4
        Re: Template where I need to know the type

        In article <21483-1067719553@jano wo.net>,
        "Jacek Dziedzic" <jacek-NOSPAM-@janowo.net> wrote:
        [color=blue]
        > Hello!
        >
        > In my code I have functions like
        >
        > SendToNode(int dest_node; int i);
        > SendToNode(int dest_node; double d);
        > SendToNode(int dest_node; unsigned int u);
        >
        > and so on, where the function body is the same for all
        > these overloaded functions, except for one line, which
        > depends on the type of the second argument to SendToNode,
        > i.e. this line looks (more or less) like
        >
        > internal_send_f unction(dest_no de, type_is_int);
        > internal_send_f unction(dest_no de, type_is_double) ;
        > internal_send_f unction(dest_no de, type_is_unsigne d_int);
        >
        > where "type_is_in t" etc. are symbolic constants of type int.
        >
        > The situation is I'm not much of an OOP programmer, still
        > it looks to me like my SendToNode function is a candidate for
        > a template. The problem is: how do I do the "branching" in the
        > template, depending on what the *type* of the argument is?
        > How do I determine the type of that argument? I tried googling
        > it up and found something called typeof(), but it looked as if
        > it was not a part of the standard (perhaps "yet").
        >
        > Is this where the concept of traits comes into play? What's
        > the story behind this?[/color]

        Maybe something like:

        template <class T>
        void
        internal_send_f unction(int dest_node, T);

        template <>
        void
        internal_send_f unction(int dest_node, int i) {}

        template <>
        void
        internal_send_f unction(int dest_node, double d) {}

        template <>
        void
        internal_send_f unction(int dest_node, unsigned int u) {}

        template <class T>
        void
        SendToNode(int dest_node, T t)
        {
        // common stuff
        internal_send_f unction(dest_no de, t);
        // common stuff
        }

        -Howard

        Comment

        Working...