dimanche 13 juin 2021

C++ Concepts/SFINAE: clang and MSVC/G++ different results for out of line function definition with template and Concept/SFINAE

I am defining a constructors of a class that constrained to check for the type equivalence of the underlying type of passed-in iterator versus the node_type defined in my class, with the help of concepts or SFINAE.

However, no matter how I tried to tweak the code using enable_if and/or concepts, I cannot make all compilers happy with out-of-line definition equal to declaration. Currently below results would yield no errors for MSVC/g++ but clang complains, all using latest version(19.28, 11.1, 12.0 respectively). Can anyone clarify what is wrong here? Code is also available on godbolt with below link. Thanks.

https://godbolt.org/z/snfrPnT8P

#include <stdio.h>
#include <iterator>
#include <array>
#include <concepts>
#include <cstdlib>



template <typename _Tp, std::size_t _N>
class PointND {
private:
    std::array<_Tp, _N> coords_;
};

template <typename _Tp, std::size_t _N, typename ElemType>
class Tree {
public:

    struct node_type {
        PointND<_Tp, _N> key;
        ElemType value;
    };
    
    template <std::random_access_iterator RAI>
    requires std::same_as<typename std::iterator_traits<RAI>::value_type, 
                          typename Tree<_Tp, _N, ElemType>::node_type>
    Tree(RAI, RAI);
};

template <typename _Tp, std::size_t _N, typename ElemType>
template <std::random_access_iterator RAI> 
requires std::same_as<typename std::iterator_traits<RAI>::value_type, 
                      typename Tree<_Tp, _N, ElemType>::node_type>
Tree<_Tp, _N, ElemType>::Tree(RAI begin, RAI end) {
    
}

Error on this line:

<source>:45:30: error: out-of-line definition of 'Tree<_Tp, _N, ElemType, DT>' does not match any declaration in 'Tree<_Tp, _N, ElemType, DT>'
Tree<_Tp, _N, ElemType, DT>::Tree(RAI begin, RAI end) {

Aucun commentaire:

Enregistrer un commentaire