Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating an object with a returned template parameter fails, why?

Tags:

c++

templates

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'
                                                     ^~
like image 831
M.Mac Avatar asked Dec 10 '25 12:12

M.Mac


1 Answers

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>
like image 154
Vittorio Romeo Avatar answered Dec 12 '25 03:12

Vittorio Romeo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!