You can declare a templated class whose definition states the default arguments, but any time you reference the class you must include all its arguments until the definition is introduced.
eg. Let's use std::vector
without including it (the second argument of std::vector
is defined with a default):
namespace std
{
template<typename, typename>
class vector;
}
#include <iostream>
template <typename S, typename T>
void Foo (const std::vector<S,T> & vector)
{
std::cout << "do vector stuff, eg., display size = "
<< vector.size() << std::endl;
}
template <typename T>
void Foo (const T & t)
{
std::cout << "do non-vector stuff..." << std::endl;
}
We can then use it without including the vector, eg.:
int main ()
{
Foo(3);
}
...Or we can use it with std::vector
, eg.:
#include <vector>
// Now the compiler understands how to handle
// std::vector with one argument
// (making use of its default argument)
int main ()
{
Foo(std::vector<int>(3));
}
I haven't checked the standards, but this works on clang
/gcc
with -std=c++98
up to -std=c++17
, so if it's not officially a standard then it looks to be unofficially so.