Назначение указателя члена с использованием троичного выраженияC++

Программы на C++. Форум разработчиков
Anonymous
Назначение указателя члена с использованием троичного выражения

Сообщение Anonymous »

Этот код компилируется нормально:

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

#include 

int main()
{
using FP = size_t (std::string::*) (const std::string &str, size_t pos) const;

FP func;

if (true)
{
func = &std::string::find_last_not_of;

} else
{
func = &std::string::find_first_not_of;
}
}
Если я попытаюсь упростить оператор if до одного выражения, например:

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

FP func = true ? &std::string::find_last_not_of : &std::string::find_first_not_of;
g++ 14.2.1 отклоняет его с помощью:

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

$ g++  c.cpp
c.cpp: In function ‘int main()’:
c.cpp:22:18: error: address of overloaded function with no contextual type information
22 |     FP f= true ? &std::string::find_last_not_of : &std::string::find_first_not_of;
|                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
c.cpp:22:51: error: address of overloaded function with no contextual type information
22 |     FP f= true ? &std::string::find_last_not_of : &std::string::find_first_not_of;
|                                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
В чем здесь проблема?

Подробнее здесь: https://stackoverflow.com/questions/793 ... expression

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