Yes, "template<typename T, T size> struct X {};" will work that way,
but you confuse me –
template<char size> struct X<char,size> {};
template<short size> struct X<short,size> {};
C++ Compiler will fail to compile -- ???
No, the last two above should compile fine - those are specializations
of the general template and, strictly speaking, they aren't needed.
If your general template doesn't fit well for all the possible types it
accepts, then you can manage different types with different
implementations by defining such specializations.
"template<typename T, T size> struct X {};" is located in header
file. I want to define member function body in source code. How do I
explicit class?
C++ Compiler will compile fine, but shows error linking.
If you're trying to separate template declarations from their
implementations be aware that you cannot - unless you have a compiler
that supports exported templates, otherwise you need to put the
definition in the header file. This is one aspect of the C++ Standard
that has not been widely implemented yet and that very likely never will be.
You need to add before linkage will work.
template struct X<char, 1>;
This is an explicit instantiation and I don't think you need it.
However, if you try to define “X< char, 5> _x” in main function body,
you will get error linking message because C++ Compiler is unable to
find constant size match. If you add “template struct X<char, 5>” in
source code, linkage will work fine.
This is strange, the line above (adding a semicolon at its end) should
work fine (without any explicit instantiation) in the depicted context,
but since the context is garbled you would be better posting the full
exact code that your compiler is rejecting - or an equivalent reduction
of the same.
How can I create constant variable for compile-time checking.
I am not able to fully understand what you are asking for.
Static asserts can be implemented using some tricks with templates, they
have been implemented "independently" (it's not that hard once you
understand the mechanism) and they also have been somewhat "formalized"
into the boost library.
Make some search for "boost static_assert" and for "boost enable_if" and
you should find some strings to follow and get further informations -
some of this could be already in your implementation in some "tr"
namespace, and some of this should be part of the next C++ Standard.