Re: class introspection

Discussion in 'C++' started by John Harrison, Jul 23, 2003.

  1. "john smith" <> wrote in message
    news:bflo3u$1gao$...
    > Hi,
    >
    > I have a class:
    >
    > template<class dim, class real_t>


    template<int dim, class real_t>

    > class Xdim {
    > public:
    > // some funcs...
    > protected:
    > real_t m_x[dim];
    > };
    > typedef Xdim<2, int> X2i;
    > typedef Xdim<2, float> X2f;
    > typedef Xdim<2, double> X2d;
    > // ... up to <3, doubl>...
    >
    > now I want to do this:
    > template<class Xdim_t> class foo {
    > public:
    > void func() {
    > for(int i = 0; i < m_something.dim; ++i) {
    > ....
    > }
    > }
    > private:
    > Xdim_t m_something;
    > };
    >
    > //main
    > foo<X4f> f;
    >
    > the problem is in void func() where I want to inspect Xdim class and find
    > out how many elements are there to loop over. I don't want to hardcode

    the
    > loop number, but since Xdim class doesn't keep the number of elements

    inside
    > the class, I don't see a way around it. However, I suppose if I can find
    > out the type that Xdim uses (int, float, double), then I can probably do
    > something like sizeof(Xdim_t)/(whatever the type is). But now the problem
    > reduces to finding out what type Xdim_t really is, and I can't see a way

    of
    > doing that either. Is there anything in the language that'll allow me to
    > either find the size, and/or the type when the class doesn't keep this
    > information around? Thanks in advance.
    >
    > Smith
    >


    You can get either the size or the type out of the Xdim class like this

    template<int dim, class real_t>
    class Xdim {
    public:
    enum { my_size = dim };
    typedef real_t my_type;
    ...
    };

    Now

    template<class Xdim_t> class foo {
    public:
    void func() {
    Xdim_t::my_size // the size
    Xdim_t::my_type // the type

    john
    John Harrison, Jul 23, 2003
    #1
    1. Advertising

  2. John Harrison

    john smith Guest

    Oops, I guess I should've clarified my original question. The suggestion
    will work if I modifiy the class, but is there a way so that I don't have to
    modify the class and be able to do what I want? The Xdim class is used
    pretty heavily by our group, and I don't want to make a specialized version.

    Thanks once again.

    Smith
    "John Harrison" <> wrote in message
    news:bflpns$gaeto$-berlin.de...
    >
    > "john smith" <> wrote in message
    > news:bflo3u$1gao$...
    > > Hi,
    > >
    > > I have a class:
    > >
    > > template<class dim, class real_t>

    >
    > template<int dim, class real_t>
    >
    > > class Xdim {
    > > public:
    > > // some funcs...
    > > protected:
    > > real_t m_x[dim];
    > > };
    > > typedef Xdim<2, int> X2i;
    > > typedef Xdim<2, float> X2f;
    > > typedef Xdim<2, double> X2d;
    > > // ... up to <3, doubl>...
    > >
    > > now I want to do this:
    > > template<class Xdim_t> class foo {
    > > public:
    > > void func() {
    > > for(int i = 0; i < m_something.dim; ++i) {
    > > ....
    > > }
    > > }
    > > private:
    > > Xdim_t m_something;
    > > };
    > >
    > > //main
    > > foo<X4f> f;
    > >
    > > the problem is in void func() where I want to inspect Xdim class and

    find
    > > out how many elements are there to loop over. I don't want to hardcode

    > the
    > > loop number, but since Xdim class doesn't keep the number of elements

    > inside
    > > the class, I don't see a way around it. However, I suppose if I can

    find
    > > out the type that Xdim uses (int, float, double), then I can probably do
    > > something like sizeof(Xdim_t)/(whatever the type is). But now the

    problem
    > > reduces to finding out what type Xdim_t really is, and I can't see a way

    > of
    > > doing that either. Is there anything in the language that'll allow me

    to
    > > either find the size, and/or the type when the class doesn't keep this
    > > information around? Thanks in advance.
    > >
    > > Smith
    > >

    >
    > You can get either the size or the type out of the Xdim class like this
    >
    > template<int dim, class real_t>
    > class Xdim {
    > public:
    > enum { my_size = dim };
    > typedef real_t my_type;
    > ...
    > };
    >
    > Now
    >
    > template<class Xdim_t> class foo {
    > public:
    > void func() {
    > Xdim_t::my_size // the size
    > Xdim_t::my_type // the type
    >
    > john
    >
    >
    john smith, Jul 24, 2003
    #2
    1. Advertising

  3. "john smith" <> wrote in message
    news:bfo2r3$2gv2$...
    > Oops, I guess I should've clarified my original question. The suggestion
    > will work if I modifiy the class, but is there a way so that I don't have

    to
    > modify the class and be able to do what I want? The Xdim class is used
    > pretty heavily by our group, and I don't want to make a specialized

    version.
    >
    > Thanks once again.
    >


    Adding one typedef and one enum hardly makes it a specialised version does
    it? It's not as if adding these things will break any existing code. Both of
    these features should have been in the original design of course.

    In any case I don't think there is any way to do this without modifying
    Xdim.

    john
    John Harrison, Jul 24, 2003
    #3
  4. "john smith" <> wrote in message
    news:bfo33i$2h16$...
    > > template<int dim, class real_t>
    > > class Xdim {
    > > public:
    > > enum { my_size = dim };
    > > typedef real_t my_type;
    > > ...
    > > };

    >
    > Can you also clarify why use an enum rather than a simple int? Thanks.
    >
    > Smith
    >


    I think you can do this

    template <int dim, class real_t>
    class Xdim
    {
    public:
    static const int my_size = dim;
    ...
    };

    template <int dim, class real_t> class Xdim::my_size;

    but not all compilers support this syntax, and its a lot more typing.

    john
    John Harrison, Jul 24, 2003
    #4
  5. >
    > Sorry
    >
    > template <int dim, class real_t> int Xdim::my_size;
    >


    Sorry again

    template <int dim, class real_t> int Xdim<dim, real_t>::my_size;

    As you can see, enum is a lot simpler.

    john
    John Harrison, Jul 24, 2003
    #5
    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. Holger Joukl
    Replies:
    1
    Views:
    302
    Jeremy Yallop
    May 7, 2004
  2. Stephen Nesbitt
    Replies:
    0
    Views:
    265
    Stephen Nesbitt
    Nov 22, 2004
  3. Mark English
    Replies:
    9
    Views:
    369
    Mike C. Fletcher
    Jan 22, 2005
  4. *binarystar*

    Introspection Class/Instance Name

    *binarystar*, Apr 26, 2006, in forum: Python
    Replies:
    4
    Views:
    349
    robert
    Apr 26, 2006
  5. Brian Munroe

    class / module introspection?

    Brian Munroe, Apr 2, 2008, in forum: Python
    Replies:
    8
    Views:
    359
    Brian Munroe
    Apr 3, 2008
Loading...

Share This Page