看看下面的例子:
#include <iostream>
#include <string>
#include <vector>
#include <type_traits>
class Stream
{
public:
template <typename T, typename = std::enable_if_t<std::is_integral<T>{} || std::is_enum<T>{}>>
Stream& append(T value)
{
this->m_str.append(std::to_string(value));
return *this;
}
Stream& append(std::string value)
{
this->m_str.append(value);
return *this;
}
template <typename T>
Stream& operator<<(T value)
{
return this->append(value);
}
std::string& str()
{
return this->m_str;
}
private:
std::string m_str;
};
int main()
{
std::vector<bool> vec = {true, true, false};
Stream stream;
for (auto it = vec.cbegin(); it != vec.cend(); ++it)
{
stream << *it;
}
stream << "test" << 12;
std::cout << stream.str() << std::endl;
}
使用gcc进行编译时可行,但不能使用clang3.8 +进行编译。
Clang似乎找不到匹配的功能:
source_file.cpp:25:22: error: no matching member function for call to 'append'
return this->append(value);
~~~~~~^~~~~~
当尝试取消引用迭代器并将bool插入流中时,问题出现在循环中 为什么它与gcc合作但不与clang合作?