C++
'std::lcm' Is Not a Member of 'std' Fix
std::lcm lives in one header, <numeric> - not in <cmath>, where a math function is expected, and not in <algorithm>. On this library version almost nothing rescues it by accident: in the sweep probed here, covering all 106 standard headers in the library's header set, only <execution> and <random> pull the name in on the side, and the textbook combination of <iostream> and <vector> does not. Like its sibling std::reduce, this error offers no note at all - no header is named, no include is suggested - so the fix has to come from knowing the name's home. This guide pins down the one failing shape, the two transitive rescues, and the one include that fixes the error by specification.
Written December 2, 2026 · Tested with GCC 13.3.0 · 22 min read
- The error: the include is missing, in a lean file
- The textbook version fails too - with no note to follow
- Neighboring headers: 103 fail the same way, and two rescue it
- The fix
Written December 2, 2026
Tested with GCC 13.3.0
What this guide is
std::lcm is a function template defined in the header <numeric>. It computes the least common multiple of two integers: if either argument is zero it returns zero, and otherwise it returns the least common multiple of the absolute values of its arguments. Its two arguments may have different integer types, and the result has their common type. It has existed since C++17 and is constexpr from its introduction. A call whose arguments are not integer types - or are bool - is ill-formed, and the behavior is undefined if an argument's absolute value, or the least common multiple of the two absolute values, is not representable in the common type.
Because the template lives in one specific header, 'lcm' is not a member of 'std' has one common cause:
- The
#include <numeric>line is missing, and no other header in the file pulls the name in transitively. For this error, in the contexts probed here, GCC prints no suggestion tail on the error line, no help lines - and no note lines at all. That silence is shared with its siblingstd::reduceand separates both fromstd::invoke, whose failing headers all carry a note naming the home header. The fix has to come from the name's home header, not from the diagnostic.
What makes this error worth separating from its relatives is where the name is expected to live. A least-common-multiple function reads like math, so the first header reached for is <cmath>, and the second is <algorithm> - and in the sweep probed here both fail, with the same bare error as every other failing header. The sweep covers every one of the 106 standard headers in this library's header set, compiled as the only header in the program, and the outcome splits only two ways - the one uniform failure, or a rescue - and the rescue set has exactly three entries: <numeric> itself, <execution>, and <random>. Its siblings in this family - std::gcd and std::reduce among them - have their own guides and are linked at the end, not re-taught here.
The error: the include is missing, in a lean file
This program returns success if the least common multiple of 12 and 18 is 36, but includes only <limits>:
#include <limits>
int main() {
return std::lcm(12, 18) == 36 ? 0 : 1;
}
$ g++ -std=c++20 a_limits.cpp -o a_limits_prog
a_limits.cpp: In function 'int main()':
a_limits.cpp:3:17: error: 'lcm' is not a member of 'std'
3 | return std::lcm(12, 18) == 36 ? 0 : 1;
| ^~~
That is the whole output: one plain error line, and nothing after it. No suggestion tail rides on the error line itself, there are no note lines, and there are no help lines. The same program with no include at all fails the same way, just as bare:
int main() {
return std::lcm(12, 18) == 36 ? 0 : 1;
}
$ g++ -std=c++20 n_none.cpp -o n_none_prog
n_none.cpp: In function 'int main()':
n_none.cpp:2:17: error: 'lcm' is not a member of 'std'
2 | return std::lcm(12, 18) == 36 ? 0 : 1;
| ^~~
The textbook version fails too - with no note to follow
The version most often written first - print the least common multiple of two values held in a vector, with std::cout - gets the same error, without <numeric>, and the same silence after it:
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {12, 18};
std::cout << std::lcm(v[0], v[1]) << "\n";
return 0;
}
$ g++ -std=c++20 t_textbook.cpp -o t_textbook_prog
t_textbook.cpp: In function 'int main()':
t_textbook.cpp:5:23: error: 'lcm' is not a member of 'std'
5 | std::cout << std::lcm(v[0], v[1]) << "\n";
| ^~~
What happened: on this library version, neither <vector> nor <iostream> pulls this name in transitively, so the call fails in a textbook file just as it does in a lean one - and there is no note pointing anywhere. In the sweep probed here, the only headers that rescue std::lcm besides <numeric> itself are <execution> and <random>, and those rescues are properties of this library version, not rules of the language. A program that uses std::lcm includes <numeric>, whether or not it happens to compile without it.
Neighboring headers: 103 fail the same way, and two rescue it
In a sweep where each of the 106 standard headers in this library's header set was compiled as the only header in a program that returns success if the least common multiple of 12 and 18 is 36, the outcome splits by compile result two ways. Every diagnostic component was classified separately - the error line, any suggestion tail on it, any note lines, and any help lines. These 103 headers all fail with the plain is not a member of 'std' error line, no suggestion tail on that line, no note lines at all, and no help lines:
<algorithm><any><array><atomic><barrier><bit><bitset><cassert><ccomplex><cctype><cerrno><cfenv><cfloat><charconv><chrono><cinttypes><ciso646><climits><clocale><cmath><codecvt><compare><complex><concepts><condition_variable><coroutine><csetjmp><csignal><cstdalign><cstdarg><cstdbool><cstddef><cstdint><cstdio><cstdlib><cstring><ctgmath><ctime><cuchar><cwchar><cwctype><deque><exception><expected><filesystem><format><forward_list><fstream><functional><future><initializer_list><iomanip><ios><iosfwd><iostream><istream><iterator><latch><limits><list><locale><map><memory><memory_resource><mutex><new><numbers><optional><ostream><queue><ranges><ratio><regex><scoped_allocator><semaphore><set><shared_mutex><source_location><span><spanstream><sstream><stack><stacktrace><stdexcept><stdfloat><stop_token><streambuf><string><string_view><syncstream><system_error><thread><tuple><type_traits><typeindex><typeinfo><unordered_map><unordered_set><utility><valarray><variant><vector><version>
A file with no include at all fails the same way, with the same absence of notes. No failing sole header prints a suggestion tail, a note line, or a help line in this sweep.
A sole <cmath> file shows the failing shape in full - and <cmath> is the header this call is most often expected in, since the name is a math function. It is not there. Neither is it in <algorithm>:
#include <cmath>
int main() {
return std::lcm(12, 18) == 36 ? 0 : 1;
}
$ g++ -std=c++20 v_cmath.cpp -o v_cmath_prog
v_cmath.cpp: In function 'int main()':
v_cmath.cpp:3:17: error: 'lcm' is not a member of 'std'
3 | return std::lcm(12, 18) == 36 ? 0 : 1;
| ^~~
#include <algorithm>
int main() {
return std::lcm(12, 18) == 36 ? 0 : 1;
}
$ g++ -std=c++20 v_algorithm.cpp -o v_algorithm_prog
v_algorithm.cpp: In function 'int main()':
v_algorithm.cpp:3:17: error: 'lcm' is not a member of 'std'
3 | return std::lcm(12, 18) == 36 ? 0 : 1;
| ^~~
And these headers rescue the program - it compiles and runs with no <numeric> anywhere in the file:
<execution><numeric><random>
Two contrasts with the neighboring sweeps are worth holding on to. First, there is no note class, no suggestion class, and no link-failure shape in this sweep: every failing sole header fails at compile time with the same bare error, so the header-naming notes that std::invoke carries - and the declared-but-not-defined link failure that std::swap produces behind five headers - have no counterpart for this name in the probed set. Second, the rescue set is exactly the one std::gcd and std::reduce - the other <numeric> names probed in this family - produced: the same two transitive rescues, <execution> and <random>. <numeric> - the home header - is on the rescue list by definition, not by transitivity, and the other two rescues are this library version's behavior, not a guarantee.
The fix
Add the header:
#include <iostream>
#include <numeric>
int main() {
std::cout << std::lcm(12, 18) << " "
<< std::lcm(6, 10) << " "
<< std::lcm(7, 13) << "\n";
return 0;
}
$ g++ -std=c++20 h_fixed.cpp -o h_fixed_prog
$ ./h_fixed_prog
36 30 91
One include, <numeric>, declares the template: 12 and 18 have least common multiple 36, 6 and 10 have 30, and the coprime pair 7 and 13 has 91 - the product of the two, since they share no factor above 1 - so the three calls are three different computations, not one answer repeated.
The fixed programs: signs are ignored, zero has a rule, and types may mix
The result is built from the absolute values of the arguments, so signs never change it - and a zero argument has an exact rule rather than an error: if either argument is zero the result is zero, whichever side the zero is on, and std::lcm(0, 0) is 0 as well:
#include <iostream>
#include <numeric>
int main() {
std::cout << std::lcm(-12, 18) << " "
<< std::lcm(12, -18) << " "
<< std::lcm(-12, -18) << " "
<< std::lcm(0, 5) << " "
<< std::lcm(5, 0) << " "
<< std::lcm(0, 0) << "\n";
return 0;
}
$ g++ -std=c++20 k_signs.cpp -o k_signs_prog
$ ./k_signs_prog
36 36 36 0 0 0
Read in order: the three signed spellings of the 12-and-18 call all report 36; std::lcm(0, 5), std::lcm(5, 0), and std::lcm(0, 0) each report 0 - a zero on either side makes the least common multiple zero by definition, not by computation. That rule is the mirror image of its sibling's: for std::gcd a single zero argument returns the other argument's absolute value, and only the both-zero call returns 0.
The two arguments do not have to share a type. The template takes two integer types and returns their common type, so a long and an int, or an int and a long long, combine directly:
#include <iostream>
#include <numeric>
int main() {
std::cout << std::lcm(12L, 18) << " "
<< std::lcm(12, 18LL) << "\n";
return 0;
}
$ g++ -std=c++20 k_mixed.cpp -o k_mixed_prog
$ ./k_mixed_prog
36 36
Both calls report 36. The common-type rule is also the boundary of the guarantee: if an argument's absolute value - or the least common multiple itself - cannot be represented in the common type, the behavior is undefined, so widening one argument is a correctness choice, not just a style one. The least common multiple grows faster than either argument, which makes that boundary easier to reach here than for std::gcd.
Versions: C++17 to call it, constexpr from the start, and no note either way
The call itself needs C++17, and unlike std::reduce - whose non-policy overloads became constexpr only in C++20 - std::lcm is constexpr from its introduction, so a static_assert over the call already compiles under C++17:
#include <numeric>
int main() {
static_assert(std::lcm(12, 18) == 36);
static_assert(std::lcm(6, -10) == 30);
return 0;
}
$ g++ -std=c++17 c_constexpr17.cpp -o c_constexpr17_prog
$ ./c_constexpr17_prog
Under C++14 the name is not there at all, even with <numeric> included - and here this error's silence costs the most, because the output is identical to the missing-include case: the same plain error, and no version note to separate a too-old standard from a missing header:
$ g++ -std=c++14 c_lcm14.cpp -o c_lcm14_prog
c_lcm14.cpp: In function 'int main()':
c_lcm14.cpp:3:17: error: 'lcm' is not a member of 'std'
3 | return std::lcm(12, 18) == 36 ? 0 : 1;
| ^~~
Its sibling std::invoke gets a note naming the standard in exactly this situation; std::lcm does not. When the include is already in the file and the error persists, the standard flag is the next thing to check - the diagnostic will not tell you.
The neighboring names: gcd and midpoint share the one header
The names std::lcm is used alongside live in the same header. std::gcd - the greatest-common-divisor counterpart, also a C++17 constexpr template over two integer types - fails in the lean context here, only <limits> included, with its own plain error and the same absence of a note. It is shown here only as a neighbor, not re-taught:
#include <limits>
int main() {
return std::gcd(12, 18) == 6 ? 0 : 1;
}
$ g++ -std=c++20 b_gcd_lean.cpp -o b_gcd_lean_prog
b_gcd_lean.cpp: In function 'int main()':
b_gcd_lean.cpp:3:17: error: 'gcd' is not a member of 'std'
3 | return std::gcd(12, 18) == 6 ? 0 : 1;
| ^~~
std::midpoint - the overflow-safe midpoint of two integers or pointers, the newer neighbor in the same header - fails the same way in the same lean context:
#include <limits>
int main() {
return std::midpoint(0, 10) == 5 ? 0 : 1;
}
$ g++ -std=c++20 b_midpoint_lean.cpp -o b_midpoint_lean_prog
b_midpoint_lean.cpp: In function 'int main()':
b_midpoint_lean.cpp:3:17: error: 'midpoint' is not a member of 'std'
3 | return std::midpoint(0, 10) == 5 ? 0 : 1;
| ^~~~~~~~
The neighboring names, fixed
Each neighboring name works under the same one added header - there is no second header to reach for in this family. The fixed neighbor program reports 6 2 5: std::gcd(12, 18) is 6, std::gcd(6, 10) is 2, and std::midpoint(0, 10) is 5:
#include <iostream>
#include <numeric>
int main() {
std::cout << std::gcd(12, 18) << " "
<< std::gcd(6, 10) << " "
<< std::midpoint(0, 10) << "\n";
return 0;
}
$ g++ -std=c++20 b_neighbours_fixed.cpp -o b_neighbours_fixed_prog
$ ./b_neighbours_fixed_prog
6 2 5
One header, <numeric>, covers the whole conversation: lcm, gcd, and midpoint all take it, and a file that uses any of them and lacks it gets the same plain error with the name swapped. One version split does run through the family: std::lcm and std::gcd are C++17 names, while std::midpoint is newer - in the programs probed here it is absent under C++17 even with <numeric> included, and present under C++20.
A different wording - and no argument-dependent rescue for this name
Including <numeric> is not the only half of the name. This program includes the header, drops the std:: qualifier, and passes plain integers:
#include <numeric>
int main() {
return lcm(12, 18) == 36 ? 0 : 1;
}
$ g++ -std=c++20 g_unqualified.cpp -o g_unqualified_prog
g_unqualified.cpp: In function 'int main()':
g_unqualified.cpp:3:12: error: 'lcm' was not declared in this scope; did you mean 'std::lcm'?
3 | return lcm(12, 18) == 36 ? 0 : 1;
| ^~~
| std::lcm
In file included from g_unqualified.cpp:1:
/usr/include/c++/13/numeric:179:5: note: 'std::lcm' declared here
179 | lcm(_Mn __m, _Nn __n) noexcept
| ^~~
That wording - was not declared in this scope, with did you mean 'std::lcm'? on the error line and a note pointing at a declaration - is a different defect with a different fix: the header is present, the qualifier is not. Qualify the name as std::lcm; adding includes does not bring the name out of namespace std.
For std::reduce, the same unqualified call compiles when its arguments are std iterators, because argument-dependent lookup finds the name from the arguments. For std::lcm there is no such case in the programs probed here: integers are fundamental types and have no associated namespace, and wrapping an argument in a std type alias does not change that - std::common_type_t<int, int> is an alias for int, so the wrapped call fails with the same wording and the same note:
#include <numeric>
#include <type_traits>
int main() {
return lcm(std::common_type_t<int, int>(12), 18) == 36 ? 0 : 1;
}
$ g++ -std=c++20 g_unqualified_common.cpp -o g_unqualified_common_prog
g_unqualified_common.cpp: In function 'int main()':
g_unqualified_common.cpp:4:12: error: 'lcm' was not declared in this scope; did you mean 'std::lcm'?
4 | return lcm(std::common_type_t<int, int>(12), 18) == 36 ? 0 : 1;
| ^~~
| std::lcm
In file included from g_unqualified_common.cpp:1:
/usr/include/c++/13/numeric:179:5: note: 'std::lcm' declared here
179 | lcm(_Mn __m, _Nn __n) noexcept
| ^~~
So for this name the qualifier rule has no exception to learn: an unqualified lcm call is always the qualifier defect, never a working shortcut.
Integer arguments only: the error that comes from inside the header
One more failure shape belongs in this guide because it is so easily mistaken for the missing-name error's family: the header is included, the name is qualified, and the call still fails - because the arguments are not integers. With double arguments the failure comes from inside <numeric> itself, as a static assertion, followed by further errors in the type machinery:
#include <numeric>
int main() {
return std::lcm(12.0, 18.0) == 36.0 ? 0 : 1;
}
$ g++ -std=c++20 g_double.cpp -o g_double_prog
In file included from g_double.cpp:1:
/usr/include/c++/13/numeric: In instantiation of 'constexpr std::common_type_t<_Tp1, _Tp2> std::lcm(_Mn, _Nn) [with _Mn = double; _Nn = double; common_type_t<_Tp1, _Tp2> = double]':
g_double.cpp:3:20: required from here
/usr/include/c++/13/numeric:181:21: error: static assertion failed: std::lcm arguments must be integers
181 | static_assert(is_integral_v<_Mn> && is_integral_v<_Nn>,
| ^~~~~~~~~~~~~~~~~~
/usr/include/c++/13/numeric:181:21: note: 'std::is_integral_v<double>' evaluates to false
In file included from /usr/include/c++/13/bits/stl_iterator_base_types.h:67,
from /usr/include/c++/13/numeric:61:
/usr/include/c++/13/type_traits: In instantiation of 'struct std::make_unsigned<double>':
/usr/include/c++/13/type_traits:1983:11: required by substitution of 'template<class _Tp> using std::make_unsigned_t = typename std::make_unsigned::type [with _Tp = double]'
/usr/include/c++/13/numeric:190:34: required from 'constexpr std::common_type_t<_Tp1, _Tp2> std::lcm(_Mn, _Nn) [with _Mn = double; _Nn = double; common_type_t<_Tp1, _Tp2> = double]'
g_double.cpp:3:20: required from here
/usr/include/c++/13/type_traits:1836:62: error: invalid use of incomplete type 'class std::__make_unsigned_selector<double, false, false>'
1836 | { typedef typename __make_unsigned_selector<_Tp>::__type type; };
| ^~~~
/usr/include/c++/13/type_traits:1744:11: note: declaration of 'class std::__make_unsigned_selector<double, false, false>'
1744 | class __make_unsigned_selector;
| ^~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/13/numeric: In instantiation of 'constexpr std::common_type_t<_Tp1, _Tp2> std::lcm(_Mn, _Nn) [with _Mn = double; _Nn = double; common_type_t<_Tp1, _Tp2> = double]':
g_double.cpp:3:20: required from here
/usr/include/c++/13/numeric:196:48: error: argument 1 in call to function '__builtin_mul_overflow' does not have integral type
196 | bool __overflow = __builtin_mul_overflow(__r, __n2, &__r);
| ^~~
A bool argument is rejected the same way, by the matching assertion - static assertion failed: std::lcm arguments must not be bool - with a further error behind it. Neither shape is an include problem: the name was found, and the arguments were refused. Convert the values to an integer type - explicitly, so the conversion is visible - rather than adding headers.
The exact rule
std::lcmis declared by#include <numeric>, and by no other header by rule. In the sweep probed here, every one of the other 103 headers in the library's header set - among them<algorithm>,<cmath>,<iostream>,<vector>,<utility>, and<version>- does not declare it, and neither does a file with no include at all.'lcm' is not a member of 'std'means the<numeric>include is missing and nothing else in the file pulled the name in - or, under a standard before C++17, that the name does not exist yet in that standard. In every context probed here, GCC prints no suggestion tail on the error line, no note lines, and no help lines for this name - the missing-include case and the too-old-standard case print identically, and only the file's own include list and standard flag separate them.- In the sweep probed here, the split is two-way: a sole header either fails with that one uniform bare shape, or rescues the program. No probed sole header produces a note class, a suggestion class, or a declared-but-not-defined link-failure shape for this name - the contrast with
std::invoke, whose failing headers all carry a note naming<functional>. - In the sweep probed here, only two headers besides
<numeric>rescuestd::lcmtransitively -<execution>and<random>, the same two that rescuestd::gcdandstd::reduce- so a file whose only include is one of those compiles and runs the call without<numeric>. That is this library version's behavior, not a guarantee - in the probed set, neither header is required to pullstd::lcmin. - The call works on absolute values and has an exact zero rule: the fixed programs here print
36 30 91forstd::lcm(12, 18),std::lcm(6, 10), andstd::lcm(7, 13), and36 36 36 0 0 0for the signed spellings of the first call, forstd::lcm(0, 5)andstd::lcm(5, 0), and forstd::lcm(0, 0)- a zero on either side gives zero, by definition. - The two arguments may have different integer types and the result has their common type: the mixed program prints
36 36forstd::lcm(12L, 18)andstd::lcm(12, 18LL). If an argument's absolute value, or the least common multiple of the two absolute values, is not representable in the common type, the behavior is undefined. std::lcmis a C++17 function template and isconstexprsince C++17: astatic_assertover the call compiles under C++17, and under C++14 the name itself is absent with the same plain error and no note.std::gcdandstd::midpointare declared by the same#include <numeric>: in the lean programs here, which include only<limits>, each gets its ownis not a member of 'std'error, each with no note - and the fixed neighbor program prints6 2 5forstd::gcd(12, 18),std::gcd(6, 10), andstd::midpoint(0, 10).'lcm' was not declared in this scopefor an unqualifiedlcmuse is a different defect: qualify the name asstd::lcm- the include alone does not bring the name out of namespacestd. For this name no argument-dependent-lookup case rescues the unqualified call in the programs probed here: integer arguments have no associated namespace, and an argument wrapped instd::common_type_tis still an integer underneath the alias, so both unqualified programs fail with the same wording.- The template is integer-only: with
<numeric>included, adoubleargument fails inside the header withstatic assertion failed: std::lcm arguments must be integers, and aboolargument with the matchingmust not be boolassertion. Those are argument defects, not include defects.
The fixes, ranked
- First, check the include list of the file that names
std::lcm. If<numeric>is not there, add it - that is the fix in the common case, and it holds whether the file failed in a lean context, failed in a textbook file, or happened to compile before through a transitive rescue. - Second, do not wait for a note to confirm the header - for this error there is none. In the contexts probed here the diagnostic is one bare line; take the fix from the name's home header,
<numeric>, not from the output. - Third, do not reach for
<cmath>because the call is a math function, or for<algorithm>because it reads like an algorithm. In the sweep probed here, both fail forstd::lcmwith the same error - the integer numeric functions live in<numeric>, alongsidestd::gcdand the range algorithms of that header. - Fourth, do not be reassured by
<iostream>,<vector>, or<string>being present - in the sweep here none of them rescues this name at all - and do not remove<numeric>because a file compiles without it: in the probed set only<execution>and<random>rescue it, and those rescues are not guaranteed. - Fifth, check the standard before changing the code when the include is already right. Under C++14 this name does not exist; under C++17 it exists and is already
constexpr, so no C++20 upgrade is needed for astatic_assertover the call. The diagnostic will not separate these cases, so read the build's standard flag directly. - Sixth, if the error says
was not declared in this scopeinstead, fix the name's qualifier tostd::lcmas well - and do not look for an argument-dependent-lookup case that makes the unqualified call work, because for integer arguments there is none. - Seventh, if the failure is a static assertion from inside
<numeric>-must be integers, ormust not be bool- the include and the qualifier are both already right: convert the arguments to an integer type, explicitly. - Eighth, if the failing name is a neighboring one, the fix is the same header:
gcdandmidpointtake the same added<numeric>- one include for the whole family.
Which fix for which cause
#include <numeric>missing,std::lcmused in a file whose only include is one of the 103 headers listed as failing in the sweep section above, or that includes nothing: the message is'lcm' is not a member of 'std', with no suggestion tail, no note lines, and no help lines - add#include <numeric>.std::lcmused in a textbook file including<iostream>and<vector>: the message is the same bare error - add#include <numeric>.std::lcmused with only<numeric>,<execution>, or<random>included: no error on this library version - add#include <numeric>anyway when it is not the include already present, because the<execution>and<random>rescues are not guaranteed.std::lcmused with<numeric>included under C++14: the message is'lcm' is not a member of 'std'with no note - compile as C++17 or later. Astatic_assertover the call needs only C++17, where the template is alreadyconstexpr.std::gcdorstd::midpointused without<numeric>in the lean context: the message names that name, with no note - add#include <numeric>. Forstd::midpointthe standard matters as well: it is absent under C++17 even with the header included, in the programs probed here.lcmused without thestd::qualifier,<numeric>included, over integer arguments - plain or wrapped instd::common_type_t: the message is'lcm' was not declared in this scope; did you mean 'std::lcm'?- qualify the name asstd::lcm.std::lcmcalled withdoubleorboolarguments,<numeric>included: the message is a static assertion from inside the header -std::lcm arguments must be integers, orstd::lcm arguments must not be bool- convert the arguments to an integer type; no include change fixes this shape.
Checklist
- Check the include list of the file that names the call, not just the project. The include must be in that file, or in a header that file includes directly by design.
- Do not read the absence of a note as a different error. For this name the missing-include diagnostic is one bare line in every probed context; the note that names the home header for
std::invokehas no counterpart here. - Do not import a neighboring family's split into this error: in this family every name -
lcm,gcd,midpoint- takes the same<numeric>, so there is no per-name header to look up. - Do not assume the textbook includes rescue this name, and do not delete a working
<numeric>line because a file compiles without it. In the sweep here<iostream>and<vector>do not rescuestd::lcmat all, and the two headers that do rescue it do so only transitively. - Match the standard to the use: the call itself needs C++17 and
<numeric>, and a constant-expression call needs nothing more - this template isconstexprfrom C++17, unlikestd::reduce, whoseconstexprarrived in C++20. - Handle the zero result before dividing by it or scaling by it:
std::lcm(0, 5)andstd::lcm(0, 0)are both defined as 0, so code that expects a nonzero common multiple needs its own guard for any zero argument. - Check the argument types before adding headers: a failure whose first error is a static assertion inside
<numeric>is an argument defect - non-integer andboolarguments are refused there, with the header already included. - After the fix, confirm the program runs, not just that it compiles: the fixed programs here print
36 30 91for the three plain calls,36 36 36 0 0 0for the signed and zero cases, and6 2 5for the neighbor calls.
Related guides
- 'std::gcd' Is Not a Member of 'std' Fix - the same missing-include shape for the greatest-common-divisor counterpart in
<numeric>: its sweep is identical in shape to this one - the same 103 uniform bare failures and the same two transitive rescues,<execution>and<random>- and the two guides differ mainly in the zero rule and the undefined-behavior boundary, since a single zero argument returns the other value forstd::gcdbut zero forstd::lcm, and the least common multiple itself can overflow the common type where a greatest common divisor cannot exceed its arguments. - 'std::reduce' Is Not a Member of 'std' Fix - the same missing-include shape for the range reduction in
<numeric>: its sweep is identical in shape to this one - the same 103 uniform bare failures and the same two transitive rescues,<execution>and<random>- and the two guides differ mainly in the version rule, sincestd::reduceisconstexpronly since C++20 wherestd::lcmisconstexprsince C++17.
Sources
- cppreference,
std::lcm— forstd::lcmbeing a function template defined in<numeric>since C++17,constexprsince C++17, that computes the least common multiple of two integers, returns zero if either argument is zero and otherwise the least common multiple of their absolute values, whose result type is the common type of the two argument types, that is ill-formed when an argument type is not an integer type or isbool, whose behavior is undefined when an argument's absolute value or the least common multiple of the two absolute values is not representable in the common type, and whose feature-test macro is__cpp_lib_gcd_lcm(201606L, C++17). - cppreference, Standard library header
<numeric>— for<numeric>being the header that declaresstd::lcmalongsidestd::gcd,std::midpoint,std::accumulate,std::reduce,std::inner_product,std::transform_reduce,std::partial_sum, the scan family, andstd::adjacent_difference. - cppreference,
std::gcd— forstd::gcdbeing a function template defined in<numeric>since C++17,constexprsince C++17, that computes the greatest common divisor of two integers, returns zero if both arguments are zero and otherwise the greatest common divisor of their absolute values, and shares the__cpp_lib_gcd_lcmfeature-test macro withstd::lcm.
Keep Exploring
Seven tips and seven puzzles so far, each one a short runnable example with the fix explained.
Paste an AddressSanitizer, UBSan, or ThreadSanitizer report and get the bug, the file and line, and the fix \u2014 in plain language.
C++20 and C++23 features, migration paths, and measured trade-offs.
The full C++ hub \u2014 language basics, the standard library, coroutines, and measured comparisons.
Paste a [Service] section and get a plain-language verdict on every sandboxing setting it finds.