Skip to content

Instantly share code, notes, and snippets.

@timmaxw
Created September 10, 2011 00:11
Show Gist options
  • Save timmaxw/1207668 to your computer and use it in GitHub Desktop.
Save timmaxw/1207668 to your computer and use it in GitHub Desktop.
C++ puzzle
#include <iostream>
/* Under GCC 4.4.1, VERSION_1 fails but VERSION_2 and VERSION_3 work. */
#ifdef VERSION_1
void show(std::string s) {
std::cout << s << std::endl;
}
template<class T>
void do_show(T t) {
show(t);
}
void show(int i) {
std::cout << i << std::endl;
}
int main(void) {
do_show(2);
return 0;
}
#endif
#ifdef VERSION_2
void show(std::string s) {
std::cout << s << std::endl;
}
void show(int i) {
std::cout << i << std::endl;
}
template<class T>
void do_show(T t) {
show(t);
}
int main(void) {
do_show(2);
return 0;
}
#endif
#ifdef VERSION_3
template<class T>
void do_show(T t) {
show(t);
}
void show(std::string s) {
std::cout << s << std::endl;
}
void show(int i) {
std::cout << i << std::endl;
}
int main(void) {
do_show(2);
return 0;
}
#endif
@aruslan
Copy link

aruslan commented Sep 13, 2011

Only VERSION_2 is valid as per 14.6.3/1 and 14.6/9.

@timmaxw
Copy link
Author

timmaxw commented Sep 13, 2011

After I posted this Gist I made a question on Stack Overflow; this excellent answer made everything clear for me. I think you're right that only VERSION_2 is valid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment