Zizheng Tai
Zizheng Tai

Reputation: 6616

Using template type with default type without angle brackets

I was playing with this example code from https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution:

#include <random>
#include <iostream>
 
int main()
{
    std::random_device rd;  //Will be used to obtain a seed for the random number engine
    std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
    std::uniform_int_distribution<> distrib(1, 6);
 
    for (int n=0; n<10; ++n)
        //Use `distrib` to transform the random unsigned int generated by gen into an int in [1, 6]
        std::cout << distrib(gen) << ' ';
    std::cout << '\n';
}

... and noticed that the following two ways both work:

std::uniform_int_distribution<> distrib(1, 6);
std::uniform_int_distribution distrib(1, 6);

However, there isn't a non-template std::uniform_int_distribution, so I assume the second one is equivalent to the first one. What is this feature (being able to omit angle brackets) called and in what version of C++ was this introduced?

Upvotes: 4

Views: 109

Answers (1)

lubgr
lubgr

Reputation: 38287

What is this feature (being able to omit angle brackets) called and in what version of C++ was this introduced?

This (somewhat controversial) feature is called class template argument deduction (short: CTAD) and has been introduced with C++17. You can read about it here. If you compile this snippet with e.g. -std=c++14, it won't compile.

Upvotes: 2

Related Questions