local type as template argument

Discussion in 'C++' started by Filip Konvicka, May 21, 2007.

  1. Hi all,

    I wonder whether anyone has seen a workaround for this somewhere,
    perhaps in Boost or elsewhere...

    I succeeded using a local type as template argument in MSVC 8.0, but I
    found out that this violates the standard (14.3.1.2), and sadly does not
    work with g++ (3.4.3, did not try newer). I was essentialy able to run
    some code for each local type before main() - see the source code below.

    I know that to make it work, one could simply make the class non-local,
    but I'd like to keep the classes local and achieve some pre-main
    initialization for them. I know that the standard is clear about this,
    but maybe there is some trick...? The reason for all this is my desire
    to see all locally-defined strings (so I can do localization etc.)

    Thanks for suggestions,
    Filip


    #include <iostream>
    #include <list>
    using namespace std;

    // common interface
    struct XC {
    virtual const char* get_data() = 0;
    virtual ~XC() {}
    };
    typedef list<XC*> XCL;
    XCL global_list;

    template<typename Y>
    struct X : public XC {
    X() {
    // make this instance globally accessible...
    // this runs before main() for each X
    // instantiation
    global_list.push_back(this);
    }
    virtual const char* get_data() {
    return get_data_static();
    }
    static X initializer;
    // return data associated with Y
    static const char* get_data_static() {
    return data;
    }
    // data associated with Y
    static const char* data;
    };

    template<typename Y>
    const char* X<Y>::data=Y::getYData();

    template<typename Y> X<Y> X<Y>::initializer;

    int main(int argc,char* argv[]) {
    // the local type whose data
    // will be visible globally
    struct Y {
    static const char* getYData() {
    return "some data";
    // make sure X<Y>::initializer exists
    X<Y>::initializer;
    }
    };
    // walk the list of X specializations:
    XCL::iterator i, e=global_list.end();
    for(i=global_list.begin(); i!=e; ++i)
    cout << (*i)->get_data() << endl;
    // use X<Y> directly:
    cout << X<Y>::get_data_static() << endl;
    return 0;
    }
     
    Filip Konvicka, May 21, 2007
    #1
    1. Advertising

  2. On 2007-05-21 08:30, Filip Konvicka wrote:
    > Hi all,
    >
    > I wonder whether anyone has seen a workaround for this somewhere,
    > perhaps in Boost or elsewhere...
    >
    > I succeeded using a local type as template argument in MSVC 8.0, but I
    > found out that this violates the standard (14.3.1.2), and sadly does not
    > work with g++ (3.4.3, did not try newer). I was essentialy able to run
    > some code for each local type before main() - see the source code below.
    >
    > I know that to make it work, one could simply make the class non-local,
    > but I'd like to keep the classes local and achieve some pre-main
    > initialization for them. I know that the standard is clear about this,
    > but maybe there is some trick...? The reason for all this is my desire
    > to see all locally-defined strings (so I can do localization etc.)


    As you said the standard is quite clear on the matter (will hopefully
    change in the next standard) so the only trick that I can come up with
    is to search the gcc documentation that enables some non-standard
    behaviour (just like MSVC8).

    --
    Erik Wikström
     
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=, May 21, 2007
    #2
    1. Advertising

  3. Filip Konvicka

    Guest

    > > I succeeded using a local type as template argument in MSVC 8.0, but I
    > > found out that this violates the standard (14.3.1.2), and sadly does not
    > > work with g++ (3.4.3, did not try newer). I was essentialy able to run
    > > some code for each local type before main() - see the source code below.
    > >
    > > I know that to make it work, one could simply make the class non-local,
    > > but I'd like to keep the classes local and achieve some pre-main
    > > initialization for them. I know that the standard is clear about this,
    > > but maybe there is some trick...? The reason for all this is my desire
    > > to see all locally-defined strings (so I can do localization etc.)

    >
    > As you said the standard is quite clear on the matter (will hopefully
    > change in the next standard) so the only trick that I can come up with
    > is to search the gcc documentation that enables some non-standard
    > behaviour (just like MSVC8).


    Thanks for your reply. gcc does not seem to have such feature (I
    checked v.4.1.2).

    Nevermind, I just wanted to make sure that I did not overlook
    something obvious. I'll stick to non-local types then, and wait
    eagerly for the next standard (I read some proposals about this, but
    it seems to me that it will take years to get into the standard, if
    ever).

    Cheers,
    Filip
     
    , May 22, 2007
    #3
    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. Gabriel Schreiber

    Error: template argument uses local type

    Gabriel Schreiber, Nov 27, 2003, in forum: C++
    Replies:
    2
    Views:
    8,766
    tom_usenet
    Nov 27, 2003
  2. IR
    Replies:
    3
    Views:
    421
  3. nw
    Replies:
    0
    Views:
    316
  4. nguillot
    Replies:
    5
    Views:
    532
  5. Ryan Dupuis
    Replies:
    8
    Views:
    1,262
    Eric Sosman
    Oct 2, 2013
Loading...

Share This Page