Home :C++: Exposing specialized templates only
Reply: 0

:C++: Exposing specialized templates only

user1336
1#
user1336 Published in June 23, 2018, 5:29 pm

I would like to create my own serialization using the following class. I want to have general protected << operator and to publish only some exactly typed specializations:

class TBinaryOut
{
public:
    virtual void write(const char * ptr, size_t count) = 0;
protected:
    template <typename T>
    TBinaryOut & operator<< (const T& i)
        { write((const char *)(void *)&i, sizeof(T)); return *this; }
public:
    template <unsigned int> TBinaryOut & operator << (const unsigned int& i);
    template <int> TBinaryOut & operator << (const int& i);
    template <uint8_t> TBinaryOut & operator << (const uint8_t& i);
    template <int8_t> TBinaryOut & operator << (const int8_t& i);
};

Unfortunately, this does not work. If I write

int A = 10;
Stream << A;

VS2013 compiler always tries to instantiate generic protected template and therefore gives an error. What should I do to make it work correctly?

Edit: If I write specializations as

template <> TBinaryOut & operator << (const unsigned int& i);

everything compiles OK, but I get unresolved linker errors for this.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.316096 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO