C++ template partial specialization error
The following code is giving me a compilation error: class Q64 is not a valid type for a template constant parameter
template<int GRIDD, class T>
INLINE T grid_residue(T amount) {
T rem = amount%(GRIDD);
if (rem > GRIDD/2) rem -= GRIDD;
return rem;
}
template<int GRIDD, Q64>
INLINE Q64 grid_residue(Q64 amount) {
return Q64(grid_residue<GRIDD, int64_t>(to_int(amount)));
}
Whats wrong? I am trying to specialize grid_residue
for class Q64
.
UPDATE:
Changed syntax. Now getting error error: functio开发者_JAVA技巧n template partial specialization 'grid_residue<GRIDD, Q64>' is not allowed
template<int GRIDD>
INLINE Q64 grid_residue(Q64 amount) {
return Q64(grid_residue<GRIDD, int>(to_int(amount)));
}
thanks
Functions cannot be partially specialized! Either use function overloading: template <int GRIDD> inline Q64 grid_residue(Q64 amount)
or wrap your function in a type (which can be partially specialized).
You cannot partially specialise functions.
struct test
{
};
template<int GRIDD, class T>
T grid_residue(T amount)
{
std::cout << "template<int GRIDD, class T> T grid_residue(T amount)" << " GRIDD: " << GRIDD << std::endl;
return T();
}
template<int GRIDD>
test grid_residue(test amount)
{
std::cout << "template<int GRIDD> test grid_residue(test amount)" << " GRIDD: " << GRIDD << std::endl;
int inp = 0;
grid_residue<GRIDD,int>(inp);
return test();
}
int
_tmain(int argc, _TCHAR* argv[])
{
test amount;
grid_residue<19>(amount);
std::string inValue;
grid_residue<19>(inValue);
}
Compiles/links ok (VS2010).
精彩评论