Выбор функций обнаружил неожиданный кандидат при использовании пространств именC++

Программы на C++. Форум разработчиков
Anonymous
Выбор функций обнаружил неожиданный кандидат при использовании пространств имен

Сообщение Anonymous »

Это продолжение этого вопроса: `Требуется выражения и пространства имен функций < /p>
Попытка спроектировать ответ, который я наткнулся на это странное поведение: < /p>

Код: Выделить всё

#include 

// dummy default declaration
template 
void foo(T) = delete;

template 
void bar(T t) {
foo(t);
};

// client types

struct S1 {};

// "specializations" for S1
void foo(S1) { std::puts("foo(S1)"); }

int main() {
foo(S1{});  // calls the expected version
bar(S1{});     // try to call the deleted version the wrong version
}
< /code>
oulpul: < /p>
foo(S1)
foo(S1)
Но если foo находится внутри пространства имен:

Код: Выделить всё

// dummy default declaration
namespace N {
template 
void foo(T) = delete;
}
template 
void bar(T t) {
N::foo(t);
};

// client types

struct S1 {};

// "specializations" for S1
namespace N {
void foo(S1) { std::puts("foo(S1)"); }
}  // namespace N

int main() {
N::foo(S1{});  // calls the expected version
bar(S1{});     // try to call the deleted version the wrong version
}
Call to bar не компилируется, как рассматриваются только первые случаи Foo (Удаленные шаблоны здесь). />

Код: Выделить всё

// a kind of trait, false by default
template struct Trait : public std::false_type
{};

template 
constexpr bool b_struct = Trait::value;

// client types

struct S1 {};
struct S2 {};

// specializations for S1
template struct Trait : public std::true_type
{};

int main() {
std::cout

Подробнее здесь: [url]https://stackoverflow.com/questions/79608912/function-selection-finding-unexpected-candidate-when-using-namespaces[/url]

Вернуться в «C++»