Init const member mit non-type template argument



  • Hi Leute,

    ich habe folgenden Code:

    enum known_types{ MSG_UNKNOWN, MSG_TEXT, MSG_FILE };	
        typedef unsigned char byte_t;
    
    	struct handler {
    
    		virtual ~handler() {}
    		virtual byte_t type() const = 0;
    		virtual void process(message& msg, queue_t& msg_queue) = 0;
    	};
    
    	template<known_types type>
    	struct typed_handler : public handler {
    		static const byte_t TYPE = type; // <-- Fehler
    
    		virtual ~typed_handler() {}
    		byte_t type() const { return TYPE;  }
    	};
    
    	class text_handler : public typed_handler<MSG_TEXT> {
    	public:
    		text_handler(std::ostream& ostream) : m_stream(ostream) {}
    	}
    

    Aber leider laesst micht mein VS2013 das nicht durchgehen mit folgender Begruendung:

    error C2057: expected constant expression

    Warum ist mein "type" keine const expression. Was ueberseh ich da grad?

    Gruessle



  • nimm mal die definition aus der klassendefinition raus.



  • asfdlol schrieb:

    nimm mal die definition aus der klassendefinition raus.

    Hmm ja das Problem hat sich wohl erledigt. Man sollte nicht 2 Sachen den gleichen Bezeichner geben:

    template<known_types type_arg> // Bezeichner geaendert
        struct typed_handler : public handler {
            static const byte_t TYPE = type_arg;
    
            virtual ~typed_handler() {}
            byte_t type() const { return TYPE;  }
        };
    

    Sorry tut mir echt leid. Leider hab ich das selber irgenwie uebersehen.

    Gruessle


Anmelden zum Antworten