Вопрос из шаблонов C++: Полное руководство, 2-е ⇐ C++
Вопрос из шаблонов C++: Полное руководство, 2-е
#include // maximum of two values of any type (call-by-reference) template T const& max (T const& a, T const& b) { return b < a ? a : b; } // maximum of two C-strings (call-by-value) char const* max (char const* a, char const* b) { return std::strcmp(b,a) < 0 ? a : b; } // maximum of three values of any type (call-by-reference) template T const& max (T const& a, T const& b, T const& c) { return max (max(a,b), c); // error if max(a,b) uses call-by-value } int main () { auto m1 = ::max(7, 42, 68); // OK char const* s1 = "frederic"; char const* s2 = "anica"; char const* s3 = "lucas"; auto m2 = ::max(s1, s2, s3); // run-time ERROR } The problem is that if you call max() for three C-strings, the statement return max (max(a,b), c); becomes a run-time error because for C-strings, max(a,b) creates a new, temporary local value that is returned by reference, but that temporary value expires as soon as the return statement is complete, leaving main() with a dangling reference.
max(a, b) call nontemplate ::max(char const* a, char const* b), why it will create a new, temporary local value.
I tested this code on VS2022, no errors.
Источник: https://stackoverflow.com/questions/781 ... -guide-2nd
#include // maximum of two values of any type (call-by-reference) template T const& max (T const& a, T const& b) { return b < a ? a : b; } // maximum of two C-strings (call-by-value) char const* max (char const* a, char const* b) { return std::strcmp(b,a) < 0 ? a : b; } // maximum of three values of any type (call-by-reference) template T const& max (T const& a, T const& b, T const& c) { return max (max(a,b), c); // error if max(a,b) uses call-by-value } int main () { auto m1 = ::max(7, 42, 68); // OK char const* s1 = "frederic"; char const* s2 = "anica"; char const* s3 = "lucas"; auto m2 = ::max(s1, s2, s3); // run-time ERROR } The problem is that if you call max() for three C-strings, the statement return max (max(a,b), c); becomes a run-time error because for C-strings, max(a,b) creates a new, temporary local value that is returned by reference, but that temporary value expires as soon as the return statement is complete, leaving main() with a dangling reference.
max(a, b) call nontemplate ::max(char const* a, char const* b), why it will create a new, temporary local value.
I tested this code on VS2022, no errors.
Источник: https://stackoverflow.com/questions/781 ... -guide-2nd
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение