Can C++ somehow accept this use of 'auto'?:
class A {
public:
A(): m_member(new auto)
{
[...]
}
private:
BoringToTypeType *m_member;
}
The purpose is to take advantage of 'auto' by simplifying the member element initialisation in A's constructor. As it is, the code raises the following error:
new expression for type 'auto' requires a constructor argument.
new auto(...)deduces the type of the resultant pointer from the expression passed inside(...). In your particular case there's nothing that can be deduced.You have a few options:
m_member(new auto(x)), wherexis an expression of typeBoringToTypeType.m_member(new std::remove_pointer_t<decltype(m_member)>), which is most certainly not an improvement overBoringToTypeType.If you don't mind defining an additional helper function, here's an alternative solution:
In this case
Tis used purely for type deduction purposes.m_memberhas to be repeated twice, but you avoid typing out its type this way.Simple tests on godbolt.org show that
newerdoes not incur any additional overhead.