Explizite Spezialisierung innerhalb einer parametrisierten Struktur



  • Hallo,

    wie gestern bin ich weiter am testen/spielen mit der Spezialisierung von Klassen/Strukturen und mache folgende Erfahrung. Dieses ist kompilierbar und ausführbar:

    template
    <
    	int I,
    	int J,
    	int K
    >
    struct foo
    {
    	public:
    
    	private:
    		int temp;
    
    		template
    		<
    			bool foo_me,
    			int O
    		>
    		struct internal_foo;
    
    		template
    		<
    			int O
    		>
    		struct internal_foo<true, O>
    		{};
    
    		template
    		<
    			int O
    		>
    		struct internal_foo<false, O>
    		{};
    };
    

    Wogegen dieses nicht ausführbar ist:

    template
    <
    	int I,
    	int J,
    	int K
    >
    struct foo
    {
    	public:
    
    	private:
    		int temp;
    
    		template
    		<
    			bool foo_me
    		>
    		struct internal_foo;
    
    		template
    		<
    		>
    		struct internal_foo<true>
    		{};
    
    		template
    		<
    		>
    		struct internal_foo<false>
    		{};
    };
    

    Zweimal kommt dieser Compiler-Fehler:

    ../src/foo3.h:32: error: invalid explicit specialization before '>' token
    ../src/foo3.h:32: error: explicit specialization in non-namespace scope 'struct foo<I, J, K>'
    ../src/foo3.h:32: error: enclosing class templates are not explicitly specialized
    ../src/foo3.h:33: error: template parameters not used in partial specialization:
    ../src/foo3.h:33: error:         'I'
    ../src/foo3.h:33: error:         'J'
    ../src/foo3.h:33: error:         'K'
    

    Zu meiner Schande muss ich gestehen, dass mir nun nicht klar ist, wie ich dieses richten kann bzw. wie I,J und K genutzt werden sollen.

    Könnt ihr mir vielleicht weiter helfen?

    Viele Grüße
    MM


  • Mod

    C++ Standard schrieb:

    14.7.3(1): An explicit specialization shall be declared in the namespace of which the template is a member, or, for member templates,
    in the namespace of which the enclosing class or enclosing class template is a member. An explicit specialization
    of a member function, member class or static data member of a class template shall be declared in the namespace of
    which the class template is a member. Such a declaration may also be a definition. If the declaration is not a definition,
    the specialization may be defined later.

    Sprich: Das ist verboten, da du die Spezialisierung im falschen Scope machst. Da es sich ohnehin nur um sinnlose Spielerei handelt, bin ich nicht sehr motiviert, eine Alternative zu suchen.



  • Hey SeppJ,

    danke für die Antwort. Kannst du mir kurz sagen ob dieses einen Lösung ist die akzeptabel ist oder ehr als Fusch bezweichnet werden kann:

    struct foo
    {
    	public:
    
    	private:
    		int temp;
    
    		template
    		<
    			bool foo_me,
    			int dummy = 0
    		>
    		struct internal_foo;
    
    		template
    		<
    			int dummy
    		>
    		struct internal_foo<true, dummy>
    		{};
    
    		template
    		<
    			int dummy
    		>
    		struct internal_foo<false, dummy>
    		{};
    };
    

    Viele Grüße
    MM


Anmelden zum Antworten