1
#include<type_traits>
#include<iostream>
using namespace std;
enum class Widget;
using UType = underlying_type_t<Widget>;
UType getWidget();
int main() {
cout<<getWidget();
cout<<static_cast<int>(Widget::a);
return 0;
}
UType getWidget() {
return static_cast<UType<Widget>>(Widget::a);
}
enum class Widget {
a, b, c
};
2
#include<iostream>
#include<type_traits>
using namespace std;
template<typename T>
using UType = underlying_type_t<T>;
enum class Widget;
template<typename T>
UType<T> getWidget();
int main() {
cout<<getWidget<Widget>();
//cout<<static_cast<int>(Widget::a);
return 0;
}
template<typename T>
UType<T> getWidget() {
return static_cast<UType<Widget>>(Widget::a);
}
enum class Widget {
a, b, c
};
When I used Widget::a in main, compiler failed because of "Undefined identifier Error". But, when I called GetWidget, this code was compiled. What is difference in two situations?
---2 is working if I use getWidget(). but 1 isn't. ---compiler : visual studio 2019 iso c++14 standard ---error message :
c2027 : use of undefined type 'Widget'
C2065 : 'a' : undeclared identifier