Compiling C ++ Template Specialization

I will talk in detail about my problem in order to explain what I'm trying to achieve, the question is in the last paragraph, if you want to ignore the details of my problem.

I have a problem with the design of the class, in which I want to pass a value of any type to a function push()and pop()that convert the value passed into a string representation that will be added to the string inside the class, effectively creating a data stream. The opposite will happen for pop(), taking the stream and converting a few bytes at the beginning of the stream back to the specified type.

Creating push()and pop()related templates stringstreamis the obvious solution. However, I want to use this functionality inside a DLL in which I can change the way the string is stored (for example, encryption or compression) without recompiling the clients. The type template Tmust be recompiled if the algorithm changes.

My next idea was to just use the features such as pushByte(), pushInt(), popByte(), popInt()etc. This would allow me to change the implementation without recompiling the clients, since they rely only on the static interface. That would be good. However, this is not so flexible. If the value was changed from byte to short, for example, all pushByte () instances corresponding to this value would need to be changed to pushShort(), similarly for popByte()- popShort(). Overloading pop()and push()to combat this can lead to conflicts in types (which will lead to explicit casting, which will ultimately lead to the same problem).

With the ideas above, I could create a working class. However, I wondered how specialized templates were compiled. If I created push<byte>()and push<short>(), it would be type-specific overloading, and changing from byte to short would automatically switch the template used, which would be ideal.

Now, to my question, if I used specialized templates only to simulate such an overload (without a type template T), could all specializations be compiled into my DLL, which would allow me to send a new implementation without recompiling the client? Or are custom templates selected or deleted in the same way as a type template Tduring client compilation?

+3
2

, . . , .

, , DLL, , . , , , ( , ) .

, , . , , , - :

int a;
byte b;

a = pop();
b = pop();

pop (, , ). - , , , :

int a;
byte b;

pop(a);
pop(b);

, , , (, , , , "pop ax" ).

+2

, 2 :

, , . , , . - . - , . boost:: variant , T (boost::variant<int, short, long, ...>). , , Ts, .

+1

Source: https://habr.com/ru/post/1755558/


All Articles