Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you explicitly provide template arguments to std::gcd?

[numeric.ops.gcd] states:

template<class M, class N>
  constexpr common_type_t<M, N> gcd(M m, N n);

Mandates: M and N both are integer types other than cv bool.

While doing so is not very useful, it seems permitted to call:

std::gcd<const int, volatile int>(0, 0);

That is because const int is an arithmetic type. However, [algorithms.requirements] paragraph 15 states:

The well-formedness and behavior of a call to an algorithm with an explicitly-specified template argument list is unspecified, except where explicitly stated otherwise.

This begs the question: Is std::gcd an algorithm, and more generally, what is an algorithm? I was unable to find a definition of "algorithm", and since [numeric.ops.gcd] is nested in [algorithm], it may be considered one.

like image 238
Jan Schultke Avatar asked Nov 30 '25 10:11

Jan Schultke


1 Answers

Yes, std::gcd is an algorithm.

Table 83 (algorithms library summary, reproduced-ish below) lists numeric.ops a.k.a. the <numeric> header as part of the library, and std::gcd falls under it (numeric.ops.gcd)

Subclause Description Header
[algorithms.requirements] Algorithms requirements
[algorithms.parallel] Parallel algorithms <execution>
[algorithms.results] Algorithm result types <algorithm>
[alg.nonmodifying] Non-modifying sequence operations
[alg.modifying.operations] Mutating sequence operations
[alg.sorting] Sorting and related operations
[numeric.ops] Generalized numeric operations <numeric>
[specialized.algorithms] Specialized <memory> algorithms <memory>
[alg.rand] Specialized <random> algorithms <random>
[alg.c.library] C library algorithms <cstdlib>
like image 186
AndyG Avatar answered Dec 02 '25 22:12

AndyG