The template default

What will the template default to if one is declared with it but not supplied when being invoked?
Last edited on
Are you talking about function templates, or class templates?
It will default to whatever you told it to default to.

e.g. std::uniform_real_distribution defaults to double.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Example program
#include <iostream>

template <typename T = int>
class Bing {
  public:
     T bong;
};

int main()
{
    Bing<> bing;
    bing.bong = 3;
    bing.bong += 0.5; // won't do anything; bong is an int
    std::cout << bing.bong << '\n'; // 3
    
    Bing<double> bing2;
    bing2.bong = 3;
    bing2.bong += 0.5;
    std::cout << bing2.bong << '\n'; // 3.5
}


For function templates, it gets more complicated.
I suggest reading: https://en.cppreference.com/w/cpp/language/template_argument_deduction
Last edited on
Topic archived. No new replies allowed.