Я столкнулся с ситуацией, когда объявление может быть недействительным. семантически допустимо, но хотелось бы избежать ошибки компиляции, пока пользователь не попытается вызвать эту функцию-член.
Например:
Код: Выделить всё
#include
template
struct A {
// how to rewrite this declaration so that it works?
typename T::value_type f(typename T::value_type x) requires std::integral {
return x * 10;
}
};
int main(){
A a; // compiles
A b; // does not compile, but we want this to compile as long as the user doesn't call `b.f`
struct B { using value_type = void; };
A c; // does not compile, but we want this to compile as long as the user doesn't call `c.f`
}
Код: Выделить всё
: In instantiation of 'struct A':
:12:12: required from here
12 | A b; // does not compile
| ^
:5:28: error: 'int' is not a class, struct, or union type
5 | typename T::value_type f(typename T::value_type x) requires std::integral {
| ^
: In instantiation of 'struct A':
:14:10: required from here
14 | A c; // does not compile
| ^
:5:28: error: invalid parameter type 'main()::B::value_type' {aka 'void'}
5 | typename T::value_type f(typename T::value_type x) requires std::integral {
| ^
:5:28: error: in declaration 'typename T::value_type A::f(typename T::value_type) requires integral'
Подробнее здесь: https://stackoverflow.com/questions/791 ... s-with-a-d