Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++17 class template deduction const-ness

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

like image 295
Fletcher Blight Avatar asked Jan 04 '23 16:01

Fletcher Blight


1 Answers

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.

like image 103
Barry Avatar answered Jan 13 '23 11:01

Barry