Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CPlusPlusNotesForProfessionals.pdf
Скачиваний:
53
Добавлен:
20.05.2023
Размер:
5.11 Mб
Скачать

Chapter 37: Function Template Overloading

Section 37.1: What is a valid function template overloading?

A function template can be overloaded under the rules for non-template function overloading (same name, but di erent parameter types) and in addition to that, the overloading is valid if

The return type is di erent, or

The template parameter list is di erent, except for the naming of parameters and the presence of default arguments (they are not part of the signature)

For a normal function, comparing two parameter types is is easy for the compiler, since it has all informat. But a type within a template may not be determined yet. Therefore, the rule for when two parameter types are equal is approximative here and says that the non-depependend types and values need to match and the spelling of dependent types and expressions needs to be the same (more precisely, they need to conform to the so-called ODR-rules), except that template parameters may be renamed. However, if under such di erent spellings, two values within the types are deemed di erent, but will always instantiate to the same values, the overloading is invalid, but no diagnostic is required from the compiler.

template<typename T> void f(T*) { }

template<typename T> void f(T) { }

This is a valid overload, as "T" and "T*" are di erent spellings. But the following is invalid, with no diagnostic required

template<typename T>

void f(T (*x)[sizeof(T) + sizeof(T)]) { }

template<typename T>

void f(T (*x)[2 * sizeof(T)]) { }

GoalKicker.com – C++ Notes for Professionals

213