I had a templated class that had a getter function and a setter function pointer. When I created an object of this class and I didn't want its setter to do anything I'd just pass nullptr as the setter function argument. Then at runtime, it would check if its setter pointer was nullptr to see whether it should call the setter.
Using a function pointer isn't good enough now because I want the class to be able to store a lambda. So instead of a function pointer the setter is now a template type argument. Before I used to pass a nullptr to signify that there was no setter, but now I can't, so I use a dummy char member, as you can see:
template <typename GetFunctionType, typename SetFunctionType = char>
class MyClass
{
public:
MyClass(GetFunctionType getFunction, SetFunctionType setFunction = char(0))
: getFunction(getFunction), setFunction(setFunction)
{}
GetFunctionType getFunction;
SetFunctionType setFunction;
typedef decltype(getFunction()) ReturnType;
void set(ReturnType value)
{
if constexpr (std::is_invocable_v<decltype(setFunction), ReturnType>)
{
setFunction(value);
std::cout << "Setter called\n";
}
else
{
// ELSE DO NOTHING
std::cout << "Object has no setter\n";
}
}
};
int main()
{
MyClass foo([]() { return 7; }, [](int val) { std::cout << "You have set the value\n"; });
MyClass foo2([]() {return 7; }); // THIS OBJECT HAS NO SETTER, BUT HAS A CHAR
// MEMBER THAT I USED AS A DUMMY
foo.set(1);
foo2.set(1);
}
My question is do I need that dummy char in cases where the object has no setter function?
Using a function pointer isn't good enough now because I want the
class to be able to store a lambda!
Not completely true!
You could store the capture-less lambdas to the typed-function pointers.
See [expr.prim.lambda.closure] (sec 7)
The closure type for a non-generic lambda-expression with no
lambda-capture whose constraints (if any) are satisfied has a
conversion function to pointer to function with C++ language linkage
having the same parameter and return types as the closure type's
function call operator.
In addition to that, normally a getter would have a signature
ReturnType /*const-ref*/ <function>();
similarly, the setter would have
void <function>(ArgumentType) /*const*/; // where ReturnType == ArgumentType usually
Combining these two information, I am suggesting the following re-structuring of your class.
(See live demo online)
#include <iostream>
#include <string>
template <typename ArgType>
class MyClass final
{
// alias types
using GetFunctionType = ArgType(*)(void);
using SetFunctionType = void(*)(ArgType);
GetFunctionType getFunction;
SetFunctionType setFunction;
public:
// Now you can set the function pointer to by default `nullptr`
MyClass(GetFunctionType getFunction = nullptr, SetFunctionType setFunction = nullptr)
: getFunction{getFunction}
, setFunction{setFunction}
{}
void set(ArgType value) const noexcept
{
if (getFunction && setFunction) // runtime nullptr check
{
setFunction(value);
std::cout << "Setter called\n\n\n";
} else {
std::cout << "Object has no setter\n\n";
}
}
};
int main()
{
// mention the argument type
MyClass<int> foo(
[]() { return 7; },
[](int val) { std::cout << "You have set the value: " << val << "\n"; }
);
MyClass<std::string> foo2([]() {return std::string{}; }); // also works
foo.set(1);
foo2.set("String");
}
It would be good if there was a way you could pass void or 'something'
so that when you declare a class member like this: T setFunction; the
compiler just removes it from the class.
To my understanding, when you do the partial specialization, you do not even need to declare the setFunctionat all.
Following is the example code, in which the first specialization
template <typename Getter, typename Setter>
class MyClass final{};
handles the when you provide Getter and Setter cases, whereas the second one handles the no setter situation.
template <typename Getter>
class MyClass<Getter, std::nullptr_t> final
Unfortunately, you still need to specify the second argument (i.e. std::nullptr_t{}) in order to choose the correct specialization.
(See live demo online)
#include <iostream>
#include <cstddef> // std::nullptr_t
template <typename Getter, typename Setter>
class MyClass final
{
Getter mGetFunction;
Setter mSetFunction;
using ReType = decltype(mGetFunction());
static_assert(std::is_invocable_v<decltype(mGetFunction)>, " Getter is not callable!");
static_assert(std::is_invocable_v<decltype(mSetFunction), ReType>, " Setter is not callable!");
public:
MyClass(Getter mGetFunction, Setter mSetFunction) noexcept
: mGetFunction{ mGetFunction }
, mSetFunction{ mSetFunction }
{}
void set(ReType value) const noexcept {
mSetFunction(value);
std::cout << "Setter called\n";
}
};
template <typename Getter>
class MyClass<Getter, std::nullptr_t> final
{
Getter mGetFunction;
using ReType = decltype(mGetFunction());
static_assert(std::is_invocable_v<decltype(mGetFunction)>, " Getter is not callable!");
public:
MyClass(Getter mGetFunction, std::nullptr_t) noexcept
: mGetFunction{ mGetFunction }
{}
void set(ReType value) const noexcept {
std::cout << "Object has no setter\n";
}
};
int main()
{
MyClass foo{
[]() { return 7; },
[](int val) { std::cout << "You have set the value\n"; }
};
foo.set(1);
//unfortunately, needed to pass second argument for class instantiation
MyClass foo2([]() {return 7; }, std::nullptr_t{});
foo2.set(1);
}
Related
I am trying to make a factory function that will be able to create objects derived from a base class using different constructors based on the given parameters. With some help from other posts here I have been able to make an example that works for a constructor that takes no parameters, but I cannot find a solution for multiple constructors.
I have the following:
#include <iostream>
#include <string>
#include <map>
#include <typeinfo>
#include <functional>
using namespace std;
class BaseObject {
public:
BaseObject(){cout<<"BaseObject def constructor\n";};
BaseObject(int type){cout<<"BaseObject non-def constructor\n";}
virtual ~BaseObject() = default;
virtual string name() = 0;
};
class Object1 : public BaseObject
{
public:
Object1(){cout<<"Object1 def constructor\n";};
Object1(int type){cout<<"Object1 non-def constructor\n";}
virtual string name() override
{
return "I am Object1";
}
};
class Object2 : public BaseObject
{
public:
Object2(){cout<<"Object2 def constructor\n";};
Object2(int type){cout<<"Object2 non-def constructor\n";}
virtual string name() override
{
return "I am Object2";
}
};
struct Factory {
public:
typedef std::map<std::string, std::function<std::unique_ptr<BaseObject>()>> FactoryMap;
template<class T>
static void register_type(const std::string & name) {
getFactoryMap()[name] = [](){ return std::make_unique<T>(); };
}
static std::unique_ptr<BaseObject> get_object(const std::string name) {
return getFactoryMap()[name]();
}
static std::unique_ptr<BaseObject> get_object(const std::string name, int type) {
return getFactoryMap()[name](type);
}
// use a singleton to prevent SIOF
static FactoryMap& getFactoryMap() {
static FactoryMap map;
return map;
}
};
int main()
{
Factory::register_type<Object1>("Object1");
Factory::register_type<Object2>("Object2");
// make Object1 using default constructor
std::unique_ptr<BaseObject> o1 = Factory::get_object("Object1");
// make Object2 using non-default constructor
std::unique_ptr<BaseObject> o2 = Factory::get_object("Object2", 1);
std::cout << o1->name() << std::endl;
std::cout << o2->name() << std::endl;
std::cout << "exit" << std::endl;
return 0;
}
Both Object1 and Object2 have two constructors (it is simplified, in practice the one with the parameter will get some saved data) and Factory has two versions of get_object() each with the name of the object to be created and the corresponding additional parameters.
The problem with the second get_object
static std::unique_ptr<BaseObject> get_object(const std::string name, int type) {
return getFactoryMap()[name](type);
}
is that the call to the constructor inside passes type parameter, but the type of the function (as defined by typedef FactoryMap) has no parameters (std::function<std::unique_ptr<BaseObject>()>).
I explored variadic templates but was not able to figure out how it should be done. One of the helpful post was this one, unforunately it does not have a full working code example.
The problem is really hard in the general case as C++ lacks reflection. It can be solved however, in the assumption that the set of available constructors is fixed for a given factory. That is if you’re fine with defining your factory like using Factory = BaseFactory<BaseObject, void(), void(int)>; it is possible albeit requires black magic more templates. (using X = Y is the new, sane form of typedef Y X, and ret(args) is a function type [not a pointer but function itself; used like void, only in pointers and templates]). For example:
template <typename Base, typename Constructor>
struct Subfactory;
template <typename Base, typename... Args>
struct Subfactory<Base, void(Args...)> {
using constructor_type = std::unique_ptr<Base>(Args&&...);
template <typename Type>
static std::unique_ptr<Base> construct(Args&&...args) {
return std::make_unique<Type>(std::forward<Args>(args)...);
}
};
template <typename Base, typename... Constructors>
struct BaseFactory {
public:
using ConstructorList = std::tuple<typename Subfactory<Base, Constructors>::constructor_type *...>;
inline static std::map<std::string, ConstructorList> types;
template<class T>
static void register_type(const std::string & name) {
types[name] = ConstructorList{Subfactory<Base, Constructors>::template construct<T>...};
}
template <typename... Args>
static std::unique_ptr<Base> make_object(const std::string name, Args&&...args) {
const ConstructorList &type = types[name];
auto constructor = std::get<std::unique_ptr<Base>(*)(Args&&...)>(type);
return constructor(std::forward<Args>(args)...);
}
};
using Factory = BaseFactory<BaseObject, void(), void(int)>;
int main()
{
Factory::register_type<Object1>("Object1");
Factory::register_type<Object2>("Object2");
// make Object1 using default constructor
std::unique_ptr<BaseObject> o1 = Factory::make_object("Object1");
// make Object2 using non-default constructor
std::unique_ptr<BaseObject> o2 = Factory::make_object("Object2", 1);
std::cout << o1->name() << std::endl;
std::cout << o2->name() << std::endl;
std::cout << "exit" << std::endl;
return 0;
}
Explanation
template <typename Base, typename Constructor>
struct Subfactory;
template <typename Base, typename... Args>
struct Subfactory<Base, void(Args...)> {
using constructor_type = std::unique_ptr<Base>(Args&&...);
template <typename Type>
static std::unique_ptr<Base> construct(Args&&...args) {
return std::make_unique<Type>(std::forward<Args>(args)...);
}
};
This is a helper to unpack a function type. Specifically, the partial specialization matches all usages of the form Subfactory<any type, void(anything here)>, filling Args... with that “anything”.
Now, the factory itself.
template <typename Base, typename... Constructors>
struct BaseFactory {
Here, Constructors... is for the list of constructor signatures
using ConstructorList = std::tuple<typename Subfactory<Base, Constructors>::constructor_type *...>;
For each element C of Constructors..., this extracts the type Subfactory<Base, C>::constructor_type *, and defines ConstructorList as a tuple of all these types.
inline static std::map<std::string, ConstructorList> types;
A nice, C++17-only (but note that make_unique is C++17 too) replacement for getFactoryMap. Optional, your getFactoryMap is equally usable.
template<class T>
static void register_type(const std::string & name) {
types[name] = ConstructorList{Subfactory<Base, Constructors>::template construct<T>...};
This instantiates Subfactory<Base, C>::construct<T> for each C from Constructors... and makes ConstructorList passing (pointers to) these functions as arguments.
template <typename... Args>
static std::unique_ptr<Base> make_object(const std::string name, Args&&...args) {
const ConstructorList &type = types[name];
auto constructor = std::get<std::unique_ptr<Base>(*)(Args&&...)>(type);
return constructor(std::forward<Args>(args)...);
This gets the “type info” (constructor list) from the map, then gets appropriate constructor [wrapper] from the tuple (based on the received arguments), and calls it.
using Factory = BaseFactory<BaseObject, void(), void(int)>;
Here, Factory is defined as a factory of BaseObject supporting constructors with no arguments and with single int argument.
Note that this solution is not perfect. It requires good match of arguments given to the factory with the arguments it supports; no overloading resolution takes place. Supporting that is likely possible but requires more complicated tricks.
Update: here is a similar solution but with proper overload resolution:
/// A wrapper over single constructor of a single type.
/// #param Base is the common type
/// #param Constructor is a function type denoting the constructor signature. It must have the form `void(constructor arguments)`
template <typename Base, typename Constructor>
struct Subfactory;
/// The only specialization of #c Subfactory
template <typename Base, typename... Args>
struct Subfactory<Base, void(Args...)> {
/// The pointer to the constructor wrapper.
std::unique_ptr<Base> (*constructor)(Args&&...args);
/// The outer constructor wrapper. Unlike #c constructor which is a variable, this one is a function thus can participate in overload resolution.
std::unique_ptr<Base> construct(Args&&...args) {
return constructor(std::forward<Args>(args)...);
}
/// A factory factory. Returns an instance able to construct an object of type #p Type, but only with a constructor accepting #p Args as arguments.
template <typename Type>
static Subfactory metafactory() {
/// The constructor wrapper (written as a lambda for compactness)
return {[](Args&&...args) -> std::unique_ptr<Base> {
return std::make_unique<Type>(std::forward<Args>(args)...);
}};
}
};
/// The generic factory.
/// #param Base is the common type. Objects are returned as pointers to that type
/// #param Constructors are function types denoting the constructor signatures. Each must have the form `void(constructor arguments)`, and they must all be distinct
template <typename Base, typename... Constructors>
struct BaseFactory {
public:
/// A wrapper on constructor list of a single type.
/// It inherits one #c Subfactory for each constructor signature from #c Constructors.
/// Could also hold additional information, if necessary.
struct TypeInfo: public Subfactory<Base, Constructors>...
{
/// Another factory factory. Returns an instance able to construct an object of type #p Type with any supported constructor.
template <typename Type>
static TypeInfo metafactory() {
return TypeInfo{
Subfactory<Base, Constructors>::template metafactory<Type>()...
};
}
/// Brings *all* constructor wrappers in the scope so that #c construct names them all, as overloaded functions.
using Subfactory<Base, Constructors>::construct...;
};
inline static std::map<std::string, TypeInfo> types;
template<class Type>
static void register_type(const std::string & name) {
types[name] = TypeInfo::template metafactory<Type>();
}
template <typename... Args>
static std::unique_ptr<Base> make_object(const std::string name, Args&&...args) {
return types[name].construct(std::forward<Args>(args)...);
}
};
/// A factory of #c BaseObject subclasses, supporting constructors taking nothing or a single int.
using Factory = BaseFactory<BaseObject, void(), void(int)>;
int main()
{
Factory::register_type<Object1>("Object1");
Factory::register_type<Object2>("Object2");
// make Object1 using default constructor
std::unique_ptr<BaseObject> o1 = Factory::make_object("Object1");
// make Object2 using non-default constructor
std::unique_ptr<BaseObject> o2 = Factory::make_object("Object2", 1);
// make Object2 using overload resolution of a non-default constructor
std::unique_ptr<BaseObject> o3 = Factory::make_object("Object2", 'c');
std::cout << o1->name() << std::endl;
std::cout << o2->name() << std::endl;
std::cout << o3->name() << std::endl;
std::cout << "exit" << std::endl;
return 0;
}
Instead of storing function pointers in a tuple, a special type, TypeInfo, is used. One pointer is stored in each its base class, all of which are Subfactory but with different template arguments. Each Subfactory defines a construct function with appropriate arguments, and TypeInfo inherits them all and makes them all visible, thus subject to overload resolution like original constructors themselves.
Inspired by the answer by #numzero, I finally adopted a solution that uses less magic/templates and thus it looks more elegant to me. This solution works for constructors having fields of complex types, on the other hand it is limited by a requirement of all BaseObject descendants to have the same set of constructors (but that was the idea from the start):
#include <iostream>
#include <string>
#include <array>
#include <vector>
#include <unordered_map>
using namespace std;
class BaseObject {
public:
virtual ~BaseObject() = default;
virtual string name() {
return "I am BaseObject";
}
};
class Object1 : public BaseObject
{
public:
Object1(const int i){cout<<"Object1 int constructor\n";}
Object1(const std::string s){cout<<"Object1 string constructor\n";}
Object1(const std::vector<double> params){cout<<"Object1 vector constructor\n";}
virtual string name() override
{
return "I am Object1";
}
};
class Object2 : public BaseObject
{
public:
Object2(const int i){cout<<"Object2 int constructor\n";}
Object2(const std::string s){cout<<"Object2 string constructor\n";}
Object2(const std::vector<double> params){cout<<"Object2 vector constructor\n";}
virtual string name() override
{
return "I am Object2";
}
};
using constructor1_t = std::function<std::unique_ptr<BaseObject>(const int)>;
using constructor2_t = std::function<std::unique_ptr<BaseObject>(const std::string s)>;
using constructor3_t = std::function<std::unique_ptr<BaseObject>(const std::vector<double>)>;
using constructors_t = std::tuple<constructor1_t, constructor2_t, constructor3_t>;
using constructors_map_t = std::unordered_map<std::string, constructors_t>;
template <class T>
std::function<constructors_t()> object_constructors = [](){
return constructors_t{
[](const int i){return std::make_unique<T>(i); },
[](const std::string s){return std::make_unique<T>(s); },
[](const std::vector<double> v){return std::make_unique<T>(v); }
};
};
constructors_map_t constructors_map = {
{"Object1", object_constructors<Object1>()},
{"Object2", object_constructors<Object2>()}
};
int main()
{
int i = 12;
std::string s = "abc";
std::vector<double> v(4, 0.0);
auto c1 = constructors_map["Object1"];
std::unique_ptr<BaseObject> o1 = std::get<0>(c1)(i);
std::unique_ptr<BaseObject> o2 = std::get<1>(c1)(s);
std::unique_ptr<BaseObject> o3 = std::get<2>(c1)(v);
std::cout << o1->name() << std::endl;
std::cout << o2->name() << std::endl;
std::cout << o3->name() << std::endl;
std::cout << "----" << std::endl;
auto c2 = constructors_map["Object2"];
std::unique_ptr<BaseObject> o4 = std::get<0>(c2)(i);
std::unique_ptr<BaseObject> o5 = std::get<1>(c2)(s);
std::unique_ptr<BaseObject> o6 = std::get<2>(c2)(v);
std::cout << o4->name() << std::endl;
std::cout << o5->name() << std::endl;
std::cout << o6->name() << std::endl;
return 0;
}
Let's say I declare a thread with the following code:
#include <thread>
#include <iostream>
void printStuff(const char* c, long x) {
std::cout << x << " bottles of " << c << " on the wall\n";
}
int main()
{
std::thread t(printStuff, "beer", 900000000);
t.join();
}
How are the arguments printStuff, "beer," and 900000000 stored in the thread?
I know they are using a variadic template, where you first pass in a function and then a parameter pack of arguments. I am confused on how they forward all these template arguments, and then somehow call the inputted function with all the arguments when join or detach is called.
std::function has similar functionality where when you call std::bind it will store a function and its arguments inside the object, and then when you call the std::function object it will just execute the bound function with its arguments.
I am basically trying to implement my own version of std::function, for my own edification. I am curious how in C++ you would go about storing a function with a bunch of arbitrary parameters inside an object, and then having a method that would call the function with the passed in arguments.
I have looked at both the thread and std::function class, and both seem to be using tuples in some way to store their arguments. In a declaration of a tuple you have to specify what types you are storing in it:
std::tuple<int, std::string> tup;
How do std::function and thread get around this by storing their variadic arguments in tuples? Furthermore, how do they retrieve the function and call it with all of the arguments?
I am basically trying to implement my own version of std::function, for my own edification. I am curious how in C++ you would go about storing a function with a bunch of arbitrary parameters inside an object, and then having a method that would call the function with the passed in arguments.
std::function is a beast of a class so I won't pretend that this is anywhere close to as complete. std::function uses type erasure and small object optimization but I'll use polymorphism and store a base class pointer to a semi-anonymous implementation of a function wrapper to show how it can be done. I say semi-anonymous because it actually has a name, but it's defined locally inside the function that instantiates it. Storing the pointer (or the empty state) will be done in a std::unique_ptr<funcbase>.
The goal, as I've understood it, is to create a class with this basic interface:
template <class R, class... Args>
class fn_with_args<R(Args...)> {
public:
template <class F> fn_with_args(F&& f, Args&&... args);
R operator()();
};
That is, we need instances of fn_with_args<R(Args...)> to be able to store function pointers / functors that when invoked with the stored arguments returns R.
#include <functional>
#include <memory>
#include <tuple>
template <class> class fn_with_args; // not implemented
template <class R, class... Args>
class fn_with_args<R(Args...)> {
// an abstract base for cloneable function objects with an operator()() to call
struct funcbase {
virtual ~funcbase() = default;
virtual std::unique_ptr<funcbase> clone() const = 0;
virtual R operator()() = 0;
};
public:
// create empty "fn_with_args":
fn_with_args() noexcept = default;
fn_with_args(std::nullptr_t) noexcept {};
// copy ctor - if store contains a pointer to a funcbase,
// let it clone itself
fn_with_args(const fn_with_args& other) :
store(other.store ? other.store->clone() : nullptr) {}
// copy assignment
fn_with_args& operator=(const fn_with_args& other) {
if(this != &other) *this = fn_with_args(other); // copy+move
return *this;
}
// moving can be done by default:
fn_with_args(fn_with_args&& other) noexcept = default;
fn_with_args& operator=(fn_with_args&& other) noexcept = default;
// constructing and storing arguments
template <class F>
fn_with_args(F&& f, Args&&... args) {
// the semi-anonymous implementation that inherits from funcbase
// and stores both the function and the arguments:
struct funcimpl : funcbase {
funcimpl(F&& f, Args&&... a)
: func{std::forward<F>(f)}, args{std::forward<Args>(a)...} {}
// cloning via a base class pointer:
std::unique_ptr<funcbase> clone() const override {
return std::make_unique<funcimpl>(*this);
}
// the operator that will call `func` with the stored arguments:
R operator()() override { return std::apply(func, args); }
F func; // the actual function/functor
std::tuple<Args...> args; // and the stored arguments
};
// create and store an instance of the above semi-anonymous class:
store = std::make_unique<funcimpl>(std::forward<F>(f),
std::forward<Args>(args)...);
}
// The call interface. It'll dereference `store` and then call it which
// will call the overridden operator()() in the semi-anonymous `funcimpl`:
R operator()() {
if(store) return (*store)();
throw std::bad_function_call();
}
private:
std::unique_ptr<funcbase> store;
};
Example usage:
#include <iostream>
double foo(int x) {
return x * 3.14159;
}
int main() {
fn_with_args<int(double)> f1([](double d) -> int { return d; }, 3.14159);
std::cout << f1() << '\n';
fn_with_args<void()> f2; // create empty
//f2(); // would throw "bad_function_call" since it is "empty"
// populate it
f2 = fn_with_args<void()>([]{ std::cout << "void\n"; });
f2();
// call regular function:
fn_with_args<double(int)> f3(foo, 2);
std::cout << f3() << '\n';
// example with capture:
int v = 123;
f1 = fn_with_args<int(double)>([v](double d) -> int { return v * d; }, 3.14159);
std::cout << f1() << '\n';
// copying:
auto f11 = f1;
std::cout << f11() << '\n'; // calling the copy
}
Demo
you should store the params in std::tuple and invoke them using std::apply
#include <functional>
#include <tuple>
#include <vector>
template <class R>
class Function_Wrapper {
public:
template <typename Callable, typename... Args>
Function_Wrapper(Callable&& callable, Args&&... args)
: fn_([=, args = std::make_tuple(std::forward<Args>(args)...)]() {
return std::apply(callable, args);
}) {}
decltype(auto) run() {
// call our callable with the passed in arguments
return fn_();
}
decltype(auto) operator()() { return run(); }
private:
std::function<R()> fn_;
};
int add(int a, int b) { return a + b; }
int main() {
std::vector<Function_Wrapper<int>> f{{&add, 9, 30}, {&add, 1, 2}};
return f[0].run() + f[1]();
}
Here in Compiler Explorer
I want to pass pointers of some functions to a template class to use them later. I was wondering if:
Does it make a possibly (speed-wise) beneficial difference if I make these functions inline?
Functions themselves are possibly one line wrapper for another functions like the example below:
//inline ?
void func_wrapper_1(){
func1(arg1);
}
//inline ?
void func_wrapper_2(){
func2(arg2);
}
and the class template is like the example below:
template<void(*f1)(), void(*f2)()>
class caller{
public:
static void func(int v){
if(v) {
(*f1)();
}else{
(*f2)();
}
}
};
And later on in the main function it will be used like the example below:
caller<func_wrapper_1,func_wrapper_2>::func(0);
caller<func_wrapper_1,func_wrapper_2>::func(1);
I know that every things depends on compiler and compiling option, but lets assume compiler accepts to make these functions inline.
Whether or not a compiler will be smart enough to inline a given situation is up for grabs but I think it may be possible by creating Callable Types by overloading the function call operator.
Something like this:
template<typename Func1, typename Func2>
class caller{
public:
static void func(int v){
if(v) {
// Func1() - creates an object of type Func1
// that object is 'called' using the '()' operator
Func1()();
}else{
Func2()();
}
}
};
struct CallableType1
{
// overloading the function call operator makes objects of
// this type callable
void operator()() const { std::cout << "callable 1" << '\n'; }
};
struct CallableType2
{
void operator()() const { std::cout << "callable 2" << '\n'; }
};
int main()
{
caller<CallableType1, CallableType2> cc;
cc.func(2);
}
I am trying to use templates to write a little test class that, for simplicity's sake, can check if any type is equal to true or equals another value.
// generic value of some sort
template<class T>
class Value {
public:
T value;
};
// generic tester class
class Test {
public:
Value value_;
Test();
~Test();
void SetValueToTest(Value value){
this->value_ = value;
}
bool IsTrue(){
return this->value_ = true;
}
template<class T>
bool IsEqual(T value){
return this->value_ = value;
}
};
So far I have this main error
error: invalid use of template-name ‘Value’ without an argument list
I think it wants me to specify the type, like <int> but that would defeat the purpose of trying to test any type as true, equal to something else, etc..
Value is not a type, it is a class template. Value<int> is a type, Value<double> is a type, etc.
You can make Test a class template to be able to use Value in it.
Here's a demonstrative program:
// generic value of some sort
template<class T>
class Value {
public:
T value;
};
// generic tester class
template<class T>
class Test {
public:
Value<T> value_;
Test() {}
~Test() {}
void SetValueToTest(Value<T> value){
this->value_ = value;
}
bool IsTrue(){
return (this->value_.value == true);
}
template<class T2>
bool IsEqual(Value<T2> value){
return (this->value_.value == value.value);
}
};
#include <iostream>
int main()
{
Test<int> a;
a.SetValueToTest(Value<int>{20});
std::cout << std::boolalpha << a.IsTrue() << std::endl;
std::cout << std::boolalpha << a.IsEqual(Value<short>{20}) << std::endl;
std::cout << std::boolalpha << a.IsEqual(Value<short>{30}) << std::endl;
}
and its output
false
true
false
Since Value is a template, you should create an instance of it by declaring the generic value, like Value<int> or Value<char>. Without that, it doesn't make any sense to the compiler.
Also in your functions you are using = (to assign a value) instead of == (to compare between values). This means you just return the Value member in Test class after assigning a new value to it.
Finally, since you called the class Value and it's member by the same name, you are trying to compare the Value class instance to true, instead of its internal value.
Fix that and your code should work.
Is it somehow possible to call every instantiation of a template function without knowing what will get instantiated at write-code-time?
#include <iostream>
template<typename T>
void print_size_of()
{
std::cout << sizeof(T) << "\n";
}
int main()
{
print_size_of<int>();
print_size_of<double>();
//won't work but maybe it shows what i want to do:
template<typename T>
print_size_of<T>();
//is there a syntax so that the compiler replaces that with `print_size_of<int>(); print_size_of<double>();`
}
This is possible; you need to add some static variable inside the function template body to record those instantiations.
In the code below, every instantiated function will have a static variable, whose constructor will register the function pointer to a global registration center:
std::vector<void(*)()> funcs;
struct helper {
explicit helper(void (*f)()) { funcs.push_back(f); }
};
template<typename T>
void print_size_of()
{
static helper _(&print_size_of<T>);
std::cout << sizeof(T) << "\n";
}
int main()
{
print_size_of<int>();
print_size_of<double>();
std::cout << "All instantiation:\n";
for ( auto f : funcs ) {
f();
}
}
EDIT:
This is not strictly recording instantiation. It only records those being called before. If you instantiate it by other methods like taking its address:
void (*f)() = &print_size_of<short>;
but don't call it, then this function pointer won't be registered.
EDIT2:
In fact, it is possible to faithfully record all instantiations. The key point is to associate the instantiation of the function template to the instantiation of a class template. Then a static member of that class will be guaranteed to initialize before entering main() function.
// this one is to make sure `funcs` is initialized
auto &get_funcs() {
static std::vector<void(*)()> funcs;
return funcs;
}
template<void (*f)()>
struct helper {
helper() { get_funcs().push_back(f); }
// this is a static class member, whose initialization is before main()
static helper _ins;
};
template<void (*f)()> helper<f> helper<f>::_ins;
template<typename T>
void print_size_of()
{
// force instantiation of a class
const void *_ = &helper<&print_size_of<T>>::_ins;
std::cout << sizeof(T) << "\n";
}
int main()
{
print_size_of<int>();
print_size_of<double>();
void (*f)() = &print_size_of<short>;
std::cout << "All instantiation:\n";
for ( auto f : get_funcs() ) {
f();
}
}
No, that is not possible.
You can get close by calling every instantiation which was already called once (Use a static variable to register on first call), but that's the best you can do.