'Error when replace template type with specify type as member variable type define
I want to change the variable type of member variable when type parameter is the specific type (ex. int to unsigned char).
The error messages are added in comment.
template <typename T>
class temClass {
private:
template<typename T> // typename : declaration of template parameter 'T' shadows template parameter
struct STR_T {
typedef T T_IF;
};
template<> // <> : error: explicit specialization in non-namespace scope 'class temClass<T>'
struct STR_T<int> { // STR_T<int> : template parameters not deducible in partial specialization
typedef unsigned char T_IF;
};
typedef typename STR_T<T>::T_IF T_DEF;
T_DEF abc;
public:
temClass() { }
~temClass() { }
void showMemSize() {
printf("mem size = %d\n", sizeof(abc));
}
};
and the main function
int main() {
temClass<int> temClassInt;
temClass<unsigned int> temClassUInt;
temClassInt.showMemSize();
temClassUInt.showMemSize();
return 0;
}
The desired result is 1 and 4.
I use eclipse CDT with MinGW GCC. the GCC version is 6.3.0.
Solution 1:[1]
i'm so dumb
thanks StoryTeller - Unslander Monica 's comment
just move STR_T to namespace
namespace NS_STR {
template<typename T2>
struct STR_T {
typedef T2 T_IF;
};
template<>
struct STR_T<int> {
typedef unsigned char T_IF;
};
}
template <typename T>
class temClass {
private:
typedef typename NS_STR::STR_T<T>::T_IF T_DEF;
T_DEF abc;
public:
temClass() { }
~temClass() { }
void showMemSize() {
printf("mem size = %d\n", sizeof(abc));
}
};
The result would be 1 and 4
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ben Li |
