Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using class constructor as callable object

Tags:

c++

c++11

class C {
public:
    C() {}
};

template<typename T>
void func(T f) {}

int main() {
    func(C);
}

How to fix the compilation error "2.cpp:9:15: error: expected primary-expression before ‘)’ token func(C);"?

Passing class as parameter seems ridiculous, but I want to compile function like thread, since "thread(C)" works fine:

#include <thread>

class C {
public:
    C() {}
};

template<typename T>
void func(T f) {}

int main() {
    std::thread(C);
}
like image 550
Yuriy Avatar asked Jun 12 '17 05:06

Yuriy


People also ask

How do you make a class object callable in Python?

Simply, you make an object callable by overriding the special method __call__() . __call__(self, arg1, .., argn, *args, **kwargs) : This method is like any other normal method in Python. It also can accept positional and arbitrary arguments.

Can you call class method in constructor?

Calling a method using this keyword from a constructor Yes, as mentioned we can call all the members of a class (methods, variables, and constructors) from instance methods or, constructors.

What is callable object in Javascript?

A callable object is a data structure that behaves as both an object and a function. Callable objects can also be thought of as stateful functions. It's not built in to the language like Python, but there are several ways to make it work. The main hurdle is giving a function object a reference to itself.

Is class A callable Python?

The callable() method returns True if the object passed is callable, False if not. In Python, classes, methods, and instances are callable because calling a class returns a new instance.

What is a callable object in Java?

One (en-US) A callable object is a data structure that behaves as both an object and a function. You can access and assign properties , as if it were a function. context. class method. Any method assigned to

What is a constructor in C++?

When a class or struct is created, its constructor is called. Constructors have the same name as the class or struct, and they usually initialize the data members of the new object. In the following example, a class named Taxi is defined by using a simple constructor. This class is then instantiated with the new operator.

How to create a callable object in Python?

An important note is that all the callable object returns the None value if nothing is returned in an explicit way. As you can see, Python defines many callable objects but you can create custom classes that return instances that are callable. The only requirement for creating a callable object is the implementation of the dunder method __call__ .

What is a class constructor in Python?

Class constructors are a fundamental part of object-oriented programming in Python. They allow you to create and properly initialize objects of a given class, making those objects ready to use. Class constructors internally trigger Python’s instantiation process, which runs through two main steps: instance creation and instance initialization.


1 Answers

Unfortunately, one cannot take address of a constructor in C++. Meaning, you cannot simply take "a pointer to constructor" and use it as a functor (like you'd do with free function or member function).

You actually cannot "pass a class" as a parameter, because C++ core does not have reflection and if you get "a class" in runtime, you cannot do anything with it. You can only "pass a class" as a template parameter.

What you can do, though, is simply use lambda function which will call the corresponding constructor:

class C {
public:
    C() {}
};

template<typename T>
void func(T f) {
  auto c_instance = f();
}

int main() {
    func([](){ return C(); });
}

Note that you still do not pass anything in runtime - return type of the lambda [](){ return C(); } is known in compile-time, therefore the type of auto c_instance is also known in compile-time.

However, I think that if you don't need to pass any other factories for C into func, simply creating the instance inside func would be clearer:

class C {
public:
    C() {}
};

template<typename T>
void func() {
  T c_instance;
}

int main() {
    func<C>();
}

Regarding std::thread(c);

std::thread(C);

is not a function call. std::thread is a type. Therefore, it declares a local variable named C. Using

auto a = std::thread(C);

should produce a similar compiler error.

like image 65
yeputons Avatar answered Sep 22 '22 17:09

yeputons