I'm getting the standard
error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion)
error. However, I have included string, and there is no apparent way for me to explain this.
Here is my code.
#include <string>
#include <vector>
#include <type_traits>
// [...]
template <typename T>
class Graph {
private:
// [...]
struct Vertex {
T name;
// [...]
};
std::vector<Vertex> verts;
public:
// [...]
template <typename P>
int vertex(P item) {
if (std::is_same<P,T>::value) {
for (unsigned int i = 0; i < verts.size(); i++){
if (verts[i].name == item) {
return i;
}
}
} else if (std::is_same<P,int>::value) {
return item;
}
return -1;
}
}
The compiler is VS2012, the platform is Windows 8.1 64bit.
Pis not a string, your code will fail. C++ is not a dynamic language - your code is evaluated at compile time. You make provisions for the case whenPis anint- indicating you expect that possibility. But in that case, your code will fail to compile, because you're still comparing it with a string.ifmakes a decision at runtime. It's not conditional compilation. The condition, in this case, is a compile-time value, but it's still used during runtime.P, my method is completely wrong? I was hopingstdcould pull off some magic with macros or something... oh well.if. You need to use meta-programming facilities likestd::enable_ifto do what you're trying to accomplish