我制作了一个用于打印一些stl容器的功能模板
#include <iostream>
#include <vector>
#include <string>
template <template <typename, typename> class C, typename T, typename A>
std::ostream& operator<<(std::ostream& os, const C<T, A>& container)
{
for (auto& elem : container)
{
os << elem << " ";
}
return os;
}
int main()
{
std::vector<std::string> v { "One", "Two", "Three" };
std::cout << v << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这可以在MSVC,Clang和ICC上按预期方式进行编译和工作,但是在使用GCC(trunk)进行编译时,会给operator<<
该行带来大量错误os << elem << " "
。并且甚至仅当使用标志-std=c++17
或进行编译时,才会出现此错误-std=c++2a
。
对于该错误,似乎是合理的std::string
,因为编译器检测到现有的函数模板,该模板对于global operator<<
接受输出流和a basic_string<CharT, Traits, …
我试图operator ++
在我的 Set 迭代器上定义来调用方法next()
,所以它会增加迭代器中的位置。
template<typename TElement>
class SetIterator {
private:
Set<TElement>& set;
int poz;
public:
SetIterator(Set<TElement>& set, int poz) : set{ set }, poz{ poz } {
while (set.elems[this->poz] == EMPTY || set.elems[this->poz] == DELETED)
this->poz++;
};
SetIterator(const SetIterator& other) = default;
~SetIterator() = default;
bool valid() {
return poz < set.capacity;
};
void next() {
poz++;
while (set.elems[poz] == EMPTY || set.elems[poz] == DELETED)
poz++;
};
SetIterator<TElement>& operator ++ () {
next();
return *this;
}; …
Run Code Online (Sandbox Code Playgroud)