I am attempting to use the new c++17 class template deduction and it all seems to work fine until I apply const. This is a small example of the trouble I'm facing:
#include <type_traits>
template <typename T>
struct X
{
T _data;
X(void) = default;
X(T && data) : _data{ data } {}
constexpr bool const_x(void) { return false; }
constexpr bool const_x(void) const { return true; }
};
template <typename T>
X(T &&) -> X<std::remove_reference_t<T>>;
int main(void)
{
X<int> a;
const X<int> b{};
X c{ 10 };
const X d{ 10 };
static_assert(!a.const_x());
static_assert(b.const_x());
static_assert(!c.const_x());
static_assert(d.const_x()); // assert fails
}
it appears that when a const X is deducing its types, the const-ness is not carried through. I know this is possible:
template <typename T>
X(T &&) -> const X<std::remove_reference_t<X>>;
but this would make every deduced type const X.
If anyone has any information or help, it would be greatly appreciated!
EDIT I am using GCC-7.1.0
This is a compiler bug - specifically gcc bug 80990. There are two separate parts here - the deduction, and the const
. The declaration:
const X d{ 10 };
will first perform class template argument deduction to pick which X
specialization d
is (so X<int>
due to the deduction guide), and then the const
is added on top of that (so X<int> const
).
Note also that this:
template <typename T>
X(T &&) -> const X<std::remove_reference_t<X>>;
is ill-formed. You cannot use cv-qualifiers there.
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