Skip to content

Instantly share code, notes, and snippets.

@Oberon00
Last active December 19, 2015 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Oberon00/5922937 to your computer and use it in GitHub Desktop.
Save Oberon00/5922937 to your computer and use it in GitHub Desktop.
C++ overload resolution and partial ordering of function template specializations
#include <iostream>
#include <cstddef>
template <std::size_t N>
void foo(char const (&)[N])
{
std::cout << "foo<" << N << ">(char const (&)["<< N << "])\n";
}
template <typename T>
void foo(T const*)
{
std::cout << "foo(T const*)";
}
int main()
{
foo("bar");
}
#include <iostream> // std::cout
#include <cstddef> // std::size_t
void foo(char const (&)[4])
{
std::cout << "foo(char const (&)["<< 4 << "])\n";
}
void foo(char const*)
{
std::cout << "foo(char const*)";
}
int main()
{
foo("bar");
}
#include <iostream>
#include <cstddef>
template <std::size_t N>
void foo(char const (&)[N])
{
std::cout << "foo<" << N << ">(char const (&)["<< N << "])\n";
}
template <typename T>
void foo(T)
{
std::cout << "foo(T)";
}
int main()
{
foo("bar");
}
#include <iostream> // std::cout
#include <cstddef> // std::size_t
template <std::size_t N>
void foo(char const (&)[N])
{
std::cout << "foo<" << N << ">(char const (&)["<< N << "])\n";
}
void foo(char const*)
{
std::cout << "foo(char const*)";
}
int main()
{
foo("bar");
}
// Output: foo(char const*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment