C++17 class template deduction const-ness

Viewed 152

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

1 Answers
Related