gpt4 book ai didi

c++ - 匿名命名空间中类的 ADL

转载 作者:行者123 更新时间:2023-11-30 04:56:20 25 4
gpt4 key购买 nike

<分区>

有谁知道为什么the next piece of code不是在 Clang 4.0.1 上编译的吗?

我有下一个错误:

call to function 'operator<<' that is neither visible in the template definition nor found by argument-dependent lookup

有一些文件test.cpp

#include <vector>
#include <iostream>


namespace Wrapper
{

template<typename T>
struct QuotedStringImpl
{
T const& Value;

explicit QuotedStringImpl(T const& value) :
Value(value)
{
}
};

template<typename T>
inline std::ostream& operator <<(std::ostream& stream, QuotedStringImpl<T> const& rhs)
{
return stream << rhs.Value;
}

template<>
inline std::ostream& operator <<(std::ostream& stream, QuotedStringImpl<std::string> const& rhs)
{
return stream << '"' << rhs.Value << '"';
}

template<typename T>
inline QuotedStringImpl<T> QuotedString(T const& value)
{
return QuotedStringImpl<T>(value);
}

template<typename T>
inline std::ostream& operator <<(std::ostream& stream, std::vector<T> const& value)
{
stream << "[";
std::copy(value.begin(), value.end(), std::ostream_iterator<T>(stream, ", "));
stream << "]";

return stream;
}

} // namespace Wrapper


namespace
{

struct Struct
{
};

std::ostream& operator<<(std::ostream& stream, Struct const&)
{
return stream << "(struct value)";
}

} // namespace

int main()
{
std::vector<Struct> collection(2);
std::cout << Wrapper::QuotedString(collection);
}

此代码使用 msvc 15 成功编译。但我在使用 Clang 4.0.1 时遇到了麻烦。根据this应该应用文档 ADL 代替实例化。但这对我不起作用。这种行为的原因是什么?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com