I want to use a template parameter from one class, to instantiate another object with the same template parameter. I tried to save the parameter inside a struct, but i cannot use the type for my new object.
template<typename D>struct Typename {
using myType = D;
};
template<typename T>
class example{
public:
Typename<T> someType;
};
int main(){
example<double> e1;//ok
example<e1.someType.myType> e2; //error: cannot refer to member 'myType'
}
The error message:
error: invalid use of ‘using myType = double’
example<e1.someType.myType> e2;
^~~~~~
error: template argument 1 is invalid
example<e1.someType.myType> e2;
^
error: conflicting declaration ‘example<double> e2’
example<typename decltype(e1.someType)::myType> e2; //error: cannot refer to member 'myType'
^~
myType is not a data member of Typename, so you cannot use member access syntax (.). You need to access the myType using the scope resolution operator (::) from the type itself. E.g.
example<decltype(e1.someType)::myType>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With