Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can CTAD be used inside a member of the template class?

C++ has this helpful feature that says that the template parameter is implicit in the code inside a template class A.
However, for construction, this seems to clash with CTAD.

How can I make CTAD take precedence?

For example, here, there is an error in the f member because A is interpreted as A<T> where T is std::string, and not deduced from the parameter double.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#include<string>

template<class T>
struct A {
    A(T const& t) {}
    auto f() {return A{5.0};}  // error here triggered by line 11 `auto a2 = a1.f();`
};

int main() {
    A a1{std::string{"hello"}};
    auto a2 = a1.f();
}

https://godbolt.org/z/3nc7nev3o

>Solution :

You need to use ::A to tell the compiler to use the name A from the global scope instead of using the name A in the class scope which is just shorthand for A<T>. That gives you:

#include<string>

template<class T>
struct A {
    A(T const& t) {}
    auto f() {return ::A{5.0};}  // uses CTAD to return A<double>
};

int main() {
    A a1{std::string{"hello"}};
    auto a2 = a1.f();
}

as seen in this live example.


Another option is to add a private alias to the class type and then use that alias in the function as needed. That gives you:

#include<string>

template<class T>
struct A {
private:
    template <typename U>
    using NewA = A<U>;
public:
    A(T const& t) {}
    auto f() {return NewA{5.0};}  // error here triggered by line 11 `auto a2 = a1.f();`
};

int main() {
    A a1{std::string{"hello"}};
    auto a2 = a1.f();
}

and can bee seen working here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading