template and static field

Discussion in 'C++' started by CHAFIK Wassime, Aug 26, 2010.

  1. hi
    how are u dealing with the heat????

    i tried to compile the following code
    /////////////////////////////////////////////////////////////////////////
    template <typename V >
    class MyTemplate{
    public:
    //typedef typename V::NestedType MyType;
    static typename V::NestedType myStaticField;
    };

    class MyV{
    public:
    class NestedType {};
    static void dummy(){};
    };

    typedef MyTemplate<MyV> MyTemplateInstance;

    template<typename V>
    MyV::NestedType MyTemplateInstance::myStaticField = MyV::NestedType();

    int main(int argc, char* argv[]){
    return 0;
    }
    ////////////////////////////////////////////////////////////////////////

    but i get the error:

    testtemplate.cpp:17: error: template arguments to
    ‘MyTemplate<MyV>::myStaticField’ do not match original template
    ‘MyTemplate<V>::myStaticField


    and i don't understand why...
    thanx in advance for your help
    CHAFIK Wassime, Aug 26, 2010
    #1
    1. Advertising

  2. * CHAFIK Wassime, on 26.08.2010 13:37:
    > hi
    > how are u dealing with the heat????


    It's slightly rainy in Oslo.


    > i tried to compile the following code
    > /////////////////////////////////////////////////////////////////////////
    > template<typename V>
    > class MyTemplate{
    > public:
    > //typedef typename V::NestedType MyType;
    > static typename V::NestedType myStaticField;
    > };
    >
    > class MyV{
    > public:
    > class NestedType {};
    > static void dummy(){};
    > };
    >
    > typedef MyTemplate<MyV> MyTemplateInstance;
    >
    > template<typename V>
    > MyV::NestedType MyTemplateInstance::myStaticField = MyV::NestedType();
    >
    > int main(int argc, char* argv[]){
    > return 0;
    > }
    > ////////////////////////////////////////////////////////////////////////
    >
    > but i get the error:
    >
    > testtemplate.cpp:17: error: template arguments to
    > ‘MyTemplate<MyV>::myStaticField’ do not match original template
    > ‘MyTemplate<V>::myStaticField
    >
    >
    > and i don't understand why...


    Make that 'MyTemplate::myStaticField', not 'MyTemplateInstance::myStaticField',
    plus change the initializer from 'MyV' to just 'V'.


    Cheers & hth.,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Aug 26, 2010
    #2
    1. Advertising

  3. CHAFIK Wassime

    lucdanton Guest

    > * CHAFIK Wassime, on 26.08.2010 13:37:
    > hi
    > how are u dealing with the heat????


    I hear it's winter on the South hemisphere (well in the temperate
    zones I guess).

    On Aug 26, 1:41 pm, "Alf P. Steinbach /Usenet" <alf.p.steinbach
    > wrote:
    > [...]
    > Make that 'MyTemplate::myStaticField', not 'MyTemplateInstance::myStaticField',
    > plus change the initializer from 'MyV' to just 'V'.
    >
    > Cheers & hth.,
    >
    > - Alf
    >
    > --
    > blog at <url:http://alfps.wordpress.com>


    That should be a qualified 'typename V' right ? Both for the type and
    the initializer.
    lucdanton, Aug 26, 2010
    #3
  4. * lucdanton, on 26.08.2010 17:32:
    > On Aug 26, 1:41 pm, "Alf P. Steinbach /Usenet"<alf.p.steinbach
    > > wrote:
    >> [...]
    >> Make that 'MyTemplate::myStaticField', not 'MyTemplateInstance::myStaticField',
    >> plus change the initializer from 'MyV' to just 'V'.
    >>

    >
    > That should be a qualified 'typename V' right ? Both for the type and
    > the initializer.


    Yah, plus also for the return type.

    By the way, an easier way is to just define an inline accessor function, like

    template< class V >
    class MyTemplate
    {
    public:
    static typename V::NestedType& someGoodName()
    {
    static typename V::NestedType theStatic;
    return theStatic;
    }
    };


    Cheers & hth.,

    - Alf

    PS: Please quote enough to establish context, and please don't quote signatures.

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Aug 26, 2010
    #4
  5. CHAFIK Wassime wrote:

    > hi
    > how are u dealing with the heat????
    >
    > i tried to compile the following code
    > /////////////////////////////////////////////////////////////////////////
    > template <typename V >
    > class MyTemplate{
    > public:
    > //typedef typename V::NestedType MyType;
    > static typename V::NestedType myStaticField;
    > };
    >
    > class MyV{
    > public:
    > class NestedType {};
    > static void dummy(){};
    > };
    >
    > typedef MyTemplate<MyV> MyTemplateInstance;
    >
    > template<typename V>
    > MyV::NestedType MyTemplateInstance::myStaticField = MyV::NestedType();
    >
    > int main(int argc, char* argv[]){
    > return 0;
    > }
    > ////////////////////////////////////////////////////////////////////////
    >
    > but i get the error:
    >
    > testtemplate.cpp:17: error: template arguments to
    > ‘MyTemplate<MyV>::myStaticField’ do not match original template
    > ‘MyTemplate<V>::myStaticField
    >
    >


    The type in the template has to match the one outside of it. The one within
    is dependent, and the one in the out-of-line definition has to be that
    dependent type too, that is, [clause-#1-param-#1]::NestedType . So, you
    need

    template <typename V >
    class MyTemplate{
    public:
    //typedef typename V::NestedType MyType;
    static typename V::NestedType myStaticField;
    };

    template<typename V>
    typename V::NestedType MyTemplate<V>::myStaticField =
    typename V::NestedType();

    class MyV{
    public:
    class NestedType {};
    static void dummy(){};
    };

    typedef MyTemplate<MyV> MyTemplateInstance;
    // no need to define anything here!
    Johannes Schaub (litb), Aug 26, 2010
    #5
  6. thanks for all
    i thought that the initialisation is left for the template instance
    thanks for your help
    CHAFIK Wassime, Aug 27, 2010
    #6
    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.

Share This Page