Tutaj mam funktora rodzaju obserwacji:Jak zmniejszyć liczbę argumentów szablonu?
template<class T, class Foo, T Foo::*p>
struct X {
void operator()(Foo & f) {
(f.*p) = 12 * (f.*p); // simple example. could be more complex `operator()`
}
};
i próbki struct:
struct FF
{
int m;
int r;
};
chcę użyć funktora X, ale nie chcę, aby jednoznacznie określić szablon argumentu następująco :
void testforx()
{
std::vector<FF> cont(5);
std::for_each(cont.begin(), cont.end(), X<int, FF, &FF::r>()); // it work, but I don't want to write `int` and `FF`
std::for_each(cont.begin(), cont.end(), createx<&FF::r>()); // how I would like to use it, how to declare `createx` function?
}
Oto co próbowałem bez powodzenia:
// that is what I tried, but it could not deduce T and Foo
template<T Foo::*p, class T, class Foo>
X<T, Foo, T Foo::*p> createx()
{
return X<T, Foo, p>();
}
// this works, but requires to write T and Foo explicitly
template<class T, class Foo, T Foo::*p>
X<T, Foo, T Foo::*p> createx()
{
return X<T, Foo, p>();
}
Mała poprawka (twój "poprawny" kod nie kompiluje się): twoja deklaracja typu powrotu 'createx' powinna zawierać' p' jako trzeci parametr szablonu, a nie 'T Foo :: * p'. Nie zmienia jednak problemu. –