Can I call a static method of a class by its name? - c++

I have a bunch of classes with the same static method name, parse. I want to create a general function that calls parse for a generic class.
Can I call the method using the string name of the class, or can I pass the class as a function parameter?

The question is very broad in my opinion. I am not sure what exactly you are after but here are some options:
Have a common interface like IParser that all subclasses must implement (in this case a method Parse). You can than have one function that takes IParser&.
If you are really after the 'call by name' like some sort of RPC than I would suggest another approach where you have to register functions manually into a map of some sort that takes a pointer to the function as value, and name of the class as key. Than calling this function is as easy as map[fnName]();

C++ has no built-in reflection mechanism. If you are interested in calling method by its string representation you need to take care of name -> method pointer mapping by yourself. In this case the std::unordered_map, std::string and std::function might be helpful.
An exemplary c++11 code:
#include <iostream>
#include <unordered_map>
#include <string>
#include <functional>
struct Foo {
static std::unordered_map<std::string, std::function<void(void)>> functions;
static void bar() {
std::cout << "Foo::bar called" << std::endl;
}
static void init() {
functions["bar"] = &Foo::bar;
}
};
std::unordered_map<std::string, std::function<void(void)> Foo::functions;
int main() {
Foo::init();
Foo::functions["bar"]();
}
However if you are not interested in calling method by string only you want to call specific method of given name you could play with templates:
#include <iostream>
struct foo {
static void bar() {
std::cout << "foo::bar called" << std::endl;
}
};
template <class T>
struct tag{};
template <class T>
void call_bar(tag<T>) {
T::bar();
}
int main() {
call_bar(tag<foo>{});
}

You only need to use the scope resolution operator :: to qualify the method with the name of the class that exposes it:
Class::Method();

Related

Determine Class Automatically When Passing Member Function Argument

I have the following C++20 structs which are used as shown:
struct Base {
template <typename C>
void wait_for(C& c, auto member) { (c.*member)(); }
};
struct Power : public Base {
void wait_for(auto member) { Base::wait_for(*this, member); }
bool isReady();
};
// called by
Power pow;
pow.wait_for(&Power::isReady);
This will be in a library so I would like to simplify the call and the need to implement wait_for in each derived class.
One simplification would be to eliminate the Power:: in the call. Is there a way to determine class type in the definition of wait_for? For example, calling with:
pow.wait_for(&isReady);
Since this is a member of Power is there a way to implicitly know this will be a member of the class?
[Okay, suspect there isn't but worth asking since C++20 has many nooks and crannies.]
There isn't, but there are ways to encapsulate the call differently, for example:
#include <concepts>
#include <functional>
#include <utility>
template<std::invocable F>
void wait_for(F&& f) { std::invoke(std::forward<F>(f)); }
//...
Power pow;
wait_for([&pow]{ pow.isReady(); });

static non-template method in template class

I want to add a static function to a template class that is accessible without passing template parameters first. Is that possible?
namespace foo {
template <typename T>
class bar {
public:
static void eggs();
};
}
foo::bar<some_t>::eggs(); // works
foo::bar::eggs(); // does not work
I would like to avoid moving eggs() to the foo namespace or to create a new namespace for it (eg. foo::bar_::eggs(), ugh).
No. That is not how template classes work. What you want to do is not possible in C++.
Remember that foo::bar does not name any type, but solely a template that can be used to create other types.
Besides using typedefs/type aliases (through using), you can perhaps have a non-templated base class for you templates, and then put your static members there. If you use public inheritance, changing the static member in any of the templated classes will change in all of them.
After experimenting with your code:
I want to add a static function to a template class that is accessible
without passing template parameters first. Is that possible?
namespace foo {
template <typename T>
class bar {
public:
static void eggs();
};
}
foo::bar<some_t>::eggs(); // works
foo::bar::eggs(); // does not work
I would like to avoid moving eggs() to the foo namespace or to create
a new namespace for it (eg. foo::bar_::eggs(), ugh).
I have come to the conclusion that, the first instance of
foo::bar<some_t>::eggs(); // works while
foo::bar::eggs(); // doesn't
Is due to the fact that when working with templates, anything within the class has to be relative to a specific object, even if you do not want the function to be. I even tried using function pointers and tried to save them to template class and without no avail I couldn't even get that to compile. I do not see much of an option for you in this situation. There maybe other tricks out there that someone might know, but not from what I can see.
You can make the template parameter optional and you can define a specialized template. Like this:
namespace foo {
template <typename T = void> class bar {
public:
static void eggs() { cout << "First there was the egg" << endl; }
};
template <> class bar<void> {
public:
static void eggs() {
cout << "Then there was the chicken... or was it?" << endl;
}
};
}
auto main() -> int {
foo::bar<int>::eggs(); // egg
foo::bar<>::eggs(); // chicken
return 0;
}

Is there a way to call an initialization function only when specific class template (specialization) is instantiated?

I'm designing a wrapper over various computational functionality. Some of the underlying backends require some init functions to be called before any other API calls are made. I could use some static variable that is initialized before main, and wrap it in some function as described here so that I can catch any errors produced during initialization.
I wonder if there is a better way to handle this. Note that there will never be an instance of the class template, as everything is either a typedef or static member.
To address the problem of initializing the API only for some specializations, and of initializing it only once, I'd do something like this:
#include <iostream>
template <typename T>
struct Wrapper
{
// class who will be statically instantiated
struct CtorClass
{
CtorClass()
{
std::cout << "Init\n";
}
};
static CtorClass static_ctor;
static void compute1(){}
static void compute2(){}
};
// definition for template static member cons
template <typename T>
typename Wrapper<T>::CtorClass Wrapper<T>::static_ctor;
struct NeedInit{};
// you will have to use static_ctor in every funcition of the
template <>
void Wrapper<NeedInit>::compute1()
{
static_ctor;
}
template <>
void Wrapper<NeedInit>::compute2()
{
static_ctor;
}
int main()
{
Wrapper<int>::compute1();
Wrapper<int>::compute2();
Wrapper<NeedInit>::compute1();
Wrapper<NeedInit>::compute2();
}
Sadly, this way you have to use static_ctor in every function specialization that belongs to a Wrapper<NeedInit> class. But you wouldn't need to check for the initialization to have already been called.
Then, you could catch errors like you said.

Storing C++ Lambda in a member variable to be used as a callback?

I'm trying to implement a c++ function that gets a Lambda callback as a parameter. The thing is, the callback is initiated asynchronously from another function in the same (called) class. I therefore need to store the Lambda in a member variable so that it can be accessed by the asynchronous function that needs to initiate the callback.
I tried all the ways I could think of to declare, set and call the Lambda using a member variable, but the code always crashes either in the assignment or in the call.
Here's a stripped-out version of what I'm trying to do.
Declaring the function:
void function(const std::function<void()>callback);
Calling the function from the main code:
myClass->function([](){cout << "Callback called";});
If I execute callback from within function it works fine, but I couldn't find a way to store it in a member variable (e.g. m_callback) and invoke it from another function of the same class.
This should work:
#include <functional>
#include <utility>
struct MyThing
{
std::function<void()> f_;
void SetCallback(std::function<void()> f) { f_ = std::move(f); }
void Action() { f_(); }
};
Usage:
#include <iostream>
MyThing thing;
thing.SetCallback([](){ std::cout << "Boo\n"; });
thing.Action();
Just create a std::function variable, and call it.
#include <iostream>
#include <functional>
struct A{
std::function<void()> cb;
void function(const std::function<void()>callback){
cb=callback;
}
};
int main() {
A a;
a.function([](){std::cout << "Callback called";});
a.cb();
}

typename when defining map data that is a function pointer with a sprinkling of templates

This is a strange question because I already know the 'coding' answer. I just want to get a better understanding of why it is so. There are guru's here who have a knack of explaining these things better than the C++ standard :)
Below we have a means to define an abstract factory template that allocates objects based on a string as a key (it is a contrived example):-
#include <iostream>
#include <map>
#include <string>
using namespace std;
template <typename T, typename TProduct>
TProduct *MyFactoryConstructHelper(const T *t)
{
if (!t) return new T;
return new T(*static_cast<const T*>(t));
}
template <typename TProduct>
class AbstractFactory
{
public:
typedef TProduct *(*MyFactoryConstructor)(const void *);
typedef map<string, MyFactoryConstructor> MyFactoryConstructorMap;
static TProduct *Create(const string &iName)
{
MyFactoryConstructor ctr = mTypes[iName];
TProduct *result = NULL;
if(ctr) result = ctr(NULL);
return result;
}
template <typename T>
static bool Register(const string &iName) {
typedef TProduct*(*ConstructPtr)(const T*);
ConstructPtr cPtr = MyFactoryConstructHelper<T, TProduct>;
string name = iName;
mTypes.insert(pair<string,MyFactoryConstructor>(name, reinterpret_cast<MyFactoryConstructor>(cPtr)));
return(true);
}
protected:
AbstractFactory() {}
static MyFactoryConstructorMap mTypes;
};
template <typename TProduct>
map<string, /*typename*/ AbstractFactory<TProduct>::MyFactoryConstructor> AbstractFactory<TProduct>::mTypes;
Here is an example of how we use it: -
class MyProduct
{
public:
virtual ~MyProduct() {}
virtual void Iam() = 0;
};
class MyProductFactory : public AbstractFactory<MyProduct>
{
public:
};
class ProductA : public MyProduct
{
public:
void Iam() { cout << "ProductA" << endl; }
};
class ProductB : public MyProduct
{
public:
void Iam() { cout << "ProductB" << endl; }
};
int _tmain(int argc, _TCHAR* argv[])
{
MyProduct *prd;
MyProductFactory::Register<ProductA>("A");
MyProductFactory::Register<ProductB>("B");
prd = MyProductFactory::Create("A");
prd->Iam();
delete prd;
prd = MyProductFactory::Create("B");
prd->Iam();
delete prd;
return 0;
}
It will not compile as is, complaining that the map does not have a valid template type argument for the data type. But if you remove the comments around the 'typename' keyword in the static member definition, everything compiles and works fine... why?
and also, can I make this any better? :)
The standard tries to allow an implementation to parse and
detect as many errors in a template as possible when it reads
the template definition, before any instantiations. C++ is not
context independent, however, and it's very difficult, if not
impossible, to correctly parse statements if you don't know
which symbols are types and which are templates. If the symbol
is dependent (depends on the template parameters in some way),
you have to tell the compiler when it is a type or a template;
otherwise, the compiler must assume that it is something else.
In this case, you're telling the compiler that
AbstractFactory::MyFactoryConstructor names a type,
and not something else.
If, when the template is instantiated, and the compiler can see
to what the symbol is really bound, it turns out that you lied
(e.g. AbstractFactory::MyFactoryConstructor is in fact
an int), then the compiler will get mad at you.
Note too that the fact that the AbstractFactory was defined
before the definition requiring the typedef doesn't change
anything. There could always be an explicit specialization for
the type you're instantiating AbstractFactory on.
The simple reason is that even though you and I looking at the code know that AbstractFactory::MyFactoryConstructor is a type, the compiler doesn't -- or rather, is prohibited by the standard from knowing this. As far as it knows in the first pass of compilation, MyFactoryConstructor -- itself inside a template yet to be fully realized -- could be something else, like a static variable, which isn't allowed as the second template argument to the map, which requires a type. Supplying "typename" permits the compiler to treat it as a type as soon as it is first encountered.