C ++ 17 templated deduction constant

I am trying to use the new C ++ 17 class template inference and it all works fine until I apply const. This is a small example of the problem I was 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 turns out that when const X infers its types, the constellation is not portable. I know this is possible:

template <typename T>
X(T &&) -> const X<std::remove_reference_t<X>>;

      

but that will make every inferred type const X.

If anyone has any information or help, we'd be grateful!

EDIT I am using GCC-7.1.0

+3


source to share


1 answer


This is a compiler error - specifically gcc error 80990 . There are two separate parts here - deduction and const

. Ad:

const X d{ 10 };

      

will first execute the inference of the class template argument to select which X

specialization d

( X<int>

sic of the subtraction guide) and then add const

on top of that (sic X<int> const

).




Please note that these are:

template <typename T>
X(T &&) -> const X<std::remove_reference_t<X>>;

      

poorly formed. You cannot use cv qualifiers there.

+8


source







All Articles