C++ Logo

std-discussion

Advanced search

CTADs and equivalent parameter list

From: tobi_at <tobi_at_[hidden]>
Date: Sun, 04 Aug 2019 11:39:18 +0200

Hi,

I think the wording of [temp.deduct.guide] (3) leaves room for interpretation. It says

"... Two deduction guide declarations in the same translation unit for the same class template shall not have equivalent parameter-declaration-clauses."

I couldn't find any definition of "equivalence" between parameter-declaration-clauses and especially none for parameter-declaration-clauses containing template-types.

So, for example, the following code may or may not be valid.

 

#include <type_traits>

 

template<typename T>

class A{

public:

    A(int){}

    A(double){}

};

template<typename T, typename = std::enable_if_t<std::is_integral_v<T>>>

A(T) -> A<int>;

template<typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>

A(T) -> A<double>;                    // <- equivalent parameter-declaration-clause as above

int main(){

    auto a = A{5};

    auto b = A{5.123};

}

 

Apparently, all major compiler's latest versions agree that the code above is valid, though MSVC 19.20 and earlier rejected it and accepted only the following *single CTAD* version:

template<typename T, typename = std::enable_if_t<std::is_integral_v<T> || std::is_floating_point_v<T>>>

A(T) -> A<typename std::conditional_t<std::is_integral_v<T>, int, double>>;

I think, the last sentence of [temp.deduct.guide] (3) should be more specific, e.g.:

"... Two deduction guide declarations in the same translation unit for the same class template shall not have equivalent parameter-declaration-clauses <new>for a given specialization</new>."

Tobias

Received on 2019-08-04 04:41:27