why dynamic argument deduction deducting it as normal integer?
Top level CV qualifiers are stripped during the type deduction. That's actually expected behavior for template function parameters that aren't a pointer or reference.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
As Narue said, the top-level cv-qualifier is discarded in function calls. This is simply because, like in your example, when you pass-by-value, the top-level cv-qualifier of the original value doesn't and shouldn't matter, because it is copied to the function anyways.
To fix the problem, simply make the parameter to print() a reference to T instead of T, as so:
#include <iostream>
template <typename T>
struct IsConst{
enum {isConst = 0};
};
template <typename T>
struct IsConst<const T>{
enum {isConst = 1};
};
template <typename T>
void print(T& val){ //notice the & here.
std::cout << IsConst<T>::isConst << std::endl; //prints 1.. hurray!!
}
int main() {
const int x = 10;
print(x); //x will be implicitly transformed to 'const int&'
std::cout << IsConst<const int>::isConst << std::endl; //prints 1
return 0;
}
mike_2000_17
Posting Virtuoso
2,134 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457