Clang (C++) 在专用化中找不到名称

clang (c++) can't find name within specialization

本文关键字:找不到 专用 C++ Clang      更新时间:2023-10-16

以下代码与gcc(g )一起编译,但对clang(c )抱怨。

我期望该行n1::generic(*it);寻找my_traits<bool>(或my_traits<const bool>)专业化,但是,它似乎正在从同一专业中寻找vector(const?)中的名称specific

另外,这是特定于布尔的。其他类型等类型的工作正常。(我尝试添加my_traits<vector<bool> >(以及const bool)专业化,但没有帮助)。

#include <vector>
namespace n1 {
    template <class T> struct my_traits { };
    template <> struct my_traits<bool> {
        static void specific(bool b) { }
    };
    template <> struct my_traits<int> {
        static void specific(int b) { }
    };
    template <typename T> void generic(const T& t)
    {
        my_traits<T>::specific(t);
    }
    template <typename T> struct my_traits<std::vector<T> > {
        static void specific(const std::vector<T>& b)
        {
            if (! b.empty()) {
                for (typename std::vector<T>::const_iterator it = b.begin();
                        it != b.end(); ++it) {
                    n1::generic(*it);
                }
            }
        }
    };
}
namespace n2 {
    struct ArrayOfBoolean {
        std::vector<bool> values;
    };
    struct ArrayOfInt {
        std::vector<int> values;
    };
}
namespace n1 {
    template<> struct my_traits<n2::ArrayOfBoolean> {
        static void specific(const n2::ArrayOfBoolean& v) {
            n1::generic(v.values);
        }
    };
    template<> struct my_traits<n2::ArrayOfInt> {
        static void specific(const n2::ArrayOfInt& v) {
            n1::generic(v.values);
        }
    };
}

c++     codec.cc   -o codec
In file included from codec.cc:1:./codec.h:17:23: error: no member named 'specific' in 'n1::my_traits<std::__1::__bit_const_reference<std::__1::vector<bool,std::__1::allocator<bool> > > >'
    my_traits<T>::specific(t);
    ~~~~~~~~~~~~~~^
./codec.h:26:25: note: in instantiation of function template specialization 'n1::generic<std::__1::__bit_const_reference<std::__1::vector<bool, std::__1::allocator<bool> > > >' requested here
    n1::generic(*it);
        ^
./codec.h:17:23: note: in instantiation of member function 'n1::my_traits<std::__1::vector<bool, std::__1::allocator<bool> > >::specific' requested here
    my_traits<T>::specific(t);
                  ^
./codec.h:47:17: note: in instantiation of function template specialization 'n1::generic<std::__1::vector<bool, std::__1::allocator<bool> > >' requested here
    n1::generic(v.values);
        ^
1 error generated.

奇数看起来, vector<bool>不能容纳任何bool s。只有您无法真正参考的位。

所以在n1::generic(*it);中没有const bool&,只有__bit_const_reference代理类。