Is it possible to put compile-time conditional code in template functions?

Kinda like,

template <class T>
void print(T &foo){
  cout << foo;
#if T == float
  cout << " is a float." << endl;
#endif
}

Something like that, I hope I made myself clear.

Thanks in Advance,

Recommended Answers

All 2 Replies

Nope, the preprocessor ONLY works before compile time, remember that, so you cant have a mix of the two :)

edit: but maby you should look at the typeid operator, it might be what you need for that example.

Err, template specialization seems to be the way to go for me, but thanks for the effort. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.