template type only known at runtime

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ralf Goertz

    template type only known at runtime

    Hi,

    I want to do able to declare an object bar of a templated class foo
    where the actual template type of foo is only know at runtime:

    -----

    #include <string>

    template <class Tclass foo {
    T x;
    public:
    void do_something_de pending_on_type _of_T(){

    ...
    };
    }

    int main(int argc, char *argv[]) {
    if (argc==1) //default type string should be used
    foo<std::string bar;
    else //use class int
    foo<intbar;
    bar.do_somethin g_depending_on_ type_of_T();
    return 0;
    }

    -----

    I know it doesn't work like this but is it possible at all?

    Ralf

  • Leandro Melo

    #2
    Re: template type only known at runtime

    On 18 nov, 08:51, Ralf Goertz
    <r_goe...@expir es-2006-11-30.arcornews.de wrote:
    Hi,
    >
    I want to do able to declare an object bar of a templated class foo
    where the actual template type of foo is only know at runtime:
    >
    -----
    >
    #include <string>
    >
    template <class Tclass foo {
        T x;
    public:
        void do_something_de pending_on_type _of_T(){
    >
            ...
        };
    >
    }
    >
    int main(int argc, char *argv[]) {
        if (argc==1) //default type string should be used
            foo<std::string bar;
        else //use class int
            foo<intbar;
        bar.do_somethin g_depending_on_ type_of_T();
        return 0;
    >
    }
    >
    Template based code is constructed at compile time (static
    polymorphism). If you need dynamic polymorphism use virtual functions.
    However, your code has a general C++ problem (not specific to
    templates). The line bar.do_somethin g_depending_on_ type_of_T(); will
    generate an error because bar is undefined. This would happen even if
    bar was not a template.

    I don't know if this is the best design for you. But the closest I can
    get to it is through template specialization:

    emplate <class Tclass foo
    {
    public:
    void do_something()
    {} //You don't need trailing semi-colon here.
    }; //You need it here though.

    template <class foo<std::string >
    {
    public:
    void do_something()
    { /*Implementation for string */ }
    };

    template <class foo<int>
    {
    public:
    void do_something()
    { /*Implementation for int */ }
    };

    int main(int argc, char *argv[])
    {
    foo<std::string stringBar;
    foo<intintBar;
    if (argc==1)
    stringBar.do_so mething();
    else
    intBar.do_somet hing();
    return 0;
    }

    --
    Leandro T. C. Melo

    Comment

    • maverik

      #3
      Re: template type only known at runtime

      On Nov 18, 1:51 pm, Ralf Goertz
      <r_goe...@expir es-2006-11-30.arcornews.de wrote:
      I know it doesn't work like this but is it possible at all?
      It wouldn't work even if you solve your problems with templates
      because of scoping:
      int main(int argc, char *argv[]) {
      if (argc==1) //default type string should be used
      foo<std::string bar;
      else //use class int
      foo<intbar;
      bar.do_somethin g_depending_on_ type_of_T();
      bar is undefined here (out of scope).
      return 0;
      }

      Comment

      • maverik

        #4
        Re: template type only known at runtime

        On Nov 18, 1:51 pm, Ralf Goertz
        <r_goe...@expir es-2006-11-30.arcornews.de wrote:
        Hi,
        >
        I want to do able to declare an object bar of a templated class foo
        where the actual template type of foo is only know at runtime:
        The most painless solution I think looks like:

        int main(int argc, char *argv[]) {
            if (argc == 1) {
                foo<std::string bar;
        bar.do_somethin g_depending_on_ type_of_T();
        } else {
                foo<intbar;
        bar.do_somethin g_depending_on_ type_of_T();
        }
            return 0;
        }

        Of course, may be a better solution also exists.

        Comment

        • Ralf Goertz

          #5
          Re: template type only known at runtime

          maverik wrote:
          On Nov 18, 1:51 pm, Ralf Goertz
          <r_goe...@expir es-2006-11-30.arcornews.de wrote:
          >
          >I know it doesn't work like this but is it possible at all?
          >
          It wouldn't work even if you solve your problems with templates
          because of scoping:
          I know about the scoping problem. The code was just there to illustrate
          what I want which was hard for me to describe in prose. The problem is
          similar to one I had a year ago or so. I had wanted to create a
          reference to cin or an ifstream object depending on how the program was
          invoked. At that time I was wondering why C++ didn't have a deferred
          intialization feature for references like

          istream &config;
          ifstream ifs;
          if (config_is_read _from_cin) config=cin; else config=ifs;

          It can be done with pointers why not with references. Anyway, it seems I
          have to use the apprach you gave in the other posting, thanks, also to
          Leandro.

          Ralf

          Comment

          • Hendrik Schober

            #6
            Re: template type only known at runtime

            Ralf Goertz wrote:
            maverik wrote:
            >
            >On Nov 18, 1:51 pm, Ralf Goertz
            ><r_goe...@expi res-2006-11-30.arcornews.de wrote:
            >>
            >>I know it doesn't work like this but is it possible at all?
            >It wouldn't work even if you solve your problems with templates
            >because of scoping:
            >
            I know about the scoping problem. The code was just there to illustrate
            what I want which was hard for me to describe in prose. The problem is
            similar to one I had a year ago or so. I had wanted to create a
            reference to cin or an ifstream object depending on how the program was
            invoked. At that time I was wondering why C++ didn't have a deferred
            intialization feature for references like
            >
            istream &config;
            ifstream ifs;
            if (config_is_read _from_cin) config=cin; else config=ifs;
            >
            It can be done with pointers why not with references. [...]
            std::istream& is = config_is_read_ from_cin ? std::cin : ifs;
            Ralf
            Schobi

            Comment

            • Hendrik Schober

              #7
              Re: template type only known at runtime

              maverik wrote:
              On Nov 18, 1:51 pm, Ralf Goertz
              <r_goe...@expir es-2006-11-30.arcornews.de wrote:
              >Hi,
              >>
              >I want to do able to declare an object bar of a templated class foo
              >where the actual template type of foo is only know at runtime:
              >
              The most painless solution I think looks like:
              >
              int main(int argc, char *argv[]) {
              if (argc == 1) {
              foo<std::string bar;
              bar.do_somethin g_depending_on_ type_of_T();
              } else {
              foo<intbar;
              bar.do_somethin g_depending_on_ type_of_T();
              }
              return 0;
              }
              >
              Of course, may be a better solution also exists.
              template< class T >
              inline void do_it() {foo<int>().do_ something_depen ding_on_type_of _T();)

              int main(int argc, char *argv[]) {
              if (argc == 1) {
              do_it<std::stri ng>();
              } else {
              do_it<int>();
              }
              return 0;
              }

              Schobi

              Comment

              • Hendrik Schober

                #8
                Re: template type only known at runtime

                Ralf Goertz wrote:
                Hi,
                >
                I want to do able to declare an object bar of a templated class foo
                where the actual template type of foo is only know at runtime:
                If the type is only known at run-time, you need run-time
                polymorphy, which is done using virtual functions and
                inheritance. However, that doesn't mean you can't use
                templates at all:

                class foo_base {
                public:
                virtual void do_something_de pending_on_type _of_T() = 0;
                };

                template< typename T >
                class foo : public foo_base {
                public:
                virtual void do_something_de pending_on_type _of_T() {}
                };
                [...]
                Ralf
                Schobi

                Comment

                • Ralf Goertz

                  #9
                  Re: template type only known at runtime

                  Hendrik Schober wrote:
                  Ralf Goertz wrote:
                  >Hi,
                  >>
                  >I want to do able to declare an object bar of a templated class foo
                  >where the actual template type of foo is only know at runtime:
                  >
                  If the type is only known at run-time, you need run-time
                  polymorphy, which is done using virtual functions and
                  inheritance. However, that doesn't mean you can't use
                  templates at all:
                  >
                  class foo_base {
                  public: virtual void do_something_de pending_on_type _of_T() = 0; };
                  virtual void do_something_de pending_on_type _of_T() = 0;
                  };
                  >
                  template< typename T >
                  class foo : public foo_base {
                  public:
                  virtual void do_something_de pending_on_type _of_T() {}
                  };

                  Actually, I just need two different types for the template,
                  foo<std::string and foo<int>. The only difference is the id type I get
                  from a database, it can be either integer or char and is known only at
                  runtime. If it is numeric I still have to do some calculations with it
                  (so I can't just sql-cast it to char and use std::string in my program).
                  Also, the way of writing back the data to the db differs because of
                  quoting. "id" must be part of the class as I also need it for
                  std::map<T,doub lewithin class foo. But wrapping it in another template
                  function as you also suggested seems to be the way to go.

                  Thanks for the ternary ?: suggestion in the other posting!

                  Ralf

                  Comment

                  Working...