I am sure this has been answered before however, I am not sure what the correct terminology/wording of my question is.
Take the following example class:
class Fooable {
public:
int foo(int a);
};
I want to write a template class that can call the foo() method of any generic type regardless if it is a pointer type or a reference as such:
template <typename T>
class FooableCaller {
public:
void useFooWithSomeContext(?) {
//This method needs to use the foo() method of some Fooable to
int a = _getA();
int b; //Returned from foo()
//Depends on if the Fooable passed as an argument is a pointer or reference type
//b = fooable.foo(a);
//b = fooable->foo(a);
_doWork(b);
}
private:
int _getA();
void _doWork(int a);
};
int main() {
Fooable f1;
std::shared_ptr<Fooable> f2 = std::make_shared<Fooable>();
//If I want to call foo() of f1, I have to do f1.foo();
//If I want to call foo() of f2, I have to do f2->foo();
//What is the correct want to write FooableCaller so that useFooWithSomeContext is select to use either . or -> depending on if T is reference or a pointer?
FooableCaller<?> fc1;
FooableCaller<?> fc2;
fc1.useFooWithSomeContext(f1);
fc2.useFooWithSomeContext(f2);
}
Is this a good place to use the std::is_pointer trait? And, if yes, how do I use it properly?
I hope this makes sense.
Aucun commentaire:
Enregistrer un commentaire