Zkus to zjednodušit na nějaký elementární příklad, který nepůjde zkompilovat, protože celou aplikaci nikdo kompilovat nebude. Jinak tohle zkompilovat jde:
#include <sstream>
#include <vector>
//--------------------------------------------------------------------------------
template <typename T, bool>
struct vector_saver
{
inline void save(size_t n, std::ostream& out_stream, const std::vector<T>& v);
};
// declartion of << which is used in the following function. Avoid lookup error.
template <typename T> inline std::ostream& operator<<(std::ostream& out_stream, const std::vector<T>& v);
//--------------------------------------------------------------------------------
/**
* Partial specialization for non-primitive types.
*/
template <typename T>
struct vector_saver<T, false>
{
inline void save(size_t n, std::ostream& out_stream, const std::vector<T>& v)
{
out_stream << n << ' ';
for (size_t i = 0; i != n; ++i)
out_stream << v[i] << ' ';
}
};
//--------------------------------------------------------------------------------
/**
* Factory that will instantiate the right functor to call depending on whether
* T is a primitive type or not.
*/
template <typename T>
inline void vector_save(size_t n, std::ostream& out_stream, const std::vector<T>& v)
{
vector_saver<T, false> saver;
saver.save(n, out_stream, v);
}
//--------------------------------------------------------------------------------
/**
* Saves the size of the vector.
*/
template <typename T>
inline std::ostream& operator<<(std::ostream& out_stream, const std::vector<T>& v)
{
vector_save(v.size(), out_stream, v);
return out_stream;
}
int main()
{
std::vector<int> v(10);
std::stringstream s;
s << v;
return 0;
}
clang --version
clang version 1.1 (Debian 2.7-3)
Target: x86_64-pc-linux-gnu
Thread model: posix
clang++ main.cc