Can I have a template on an instance method in CPP? - c++

I want to do the following (unless there's just a better way that's more C++-ey)
class A
{
...
template<typename T>
<const T> methodName(args);
}
So I can use it as following:
A myObj;
myObj->methodName<myTypeName>(args);
But this isn't the syntax of the method call.
What's the correct way to write this? For some details the code is intended to consume a message type for which the object holds the raw data. The raw data is then decoded according to the message type and the data is stored on the object.

What's the correct way to write this?
You have surrounded const T with angle brackets <> which is not needed. Moreover, you've used -> instead of using . when calling the member function.
class A
{
public:
//----------------------vvvvvvv--------------------->angle brackets removed from around const T
template<typename T> const T methodName(T arg)
{
std::cout<<"methodName called"<<std::endl;
return arg;
}
};
int main()
{
A myObj;
//-------v---------------------->used . instead of ->
myObj.methodName<int>(4);
//------------------v------->no need for <double> here as template argument deduction can be used automatically
myObj.methodName(5.5);
return 0;
}
Working demo

Related

Can I associate one class with another from a template (using C++17 variant)?

I have some code that accepts one type of object and creates another type of object based on the type of the first. (There is a 1->1 relationship between the types.) I originally used a hash table (unordered_map<>) with a key based on the type of the first object to associate a creation function for the second object. But as I am learning more about the C++ features introduced since the last time I was full-time C++, I discovered std::variant<>.
I have successfully converted the implementation to use this C++17 feature. However, there is one remaining piece that is still a bit cumbersome. The design makes a call to a static member function of the second class to validate the contents of the first object, before instantiating an object of the second class. To handle this right now, I'm using a visitor structure with function operators overloaded for each input type.
What I'm wondering is if there is some way to use a template for the association, rather than the copied code with only the types different?
I've tried looking at the way std::variant<> works, and I see where the index of the type can be obtained with .index(). I can see how to instantiate an object based on an index, which I might use if I created a second std::variant<> with the object types. But, as you can see, I don't want to instantiate the object until the parameters have been validated. The function that does that is static, and I don't see a way to associate the parms type with the object type in a way that lets me make the static call.
(I also realize that these two visitor structures can be combined in the code below, but in the real code, the creation is longer and more complicated, and I would rather not have copies of it in each overload.)
struct Type1Parms {};
struct Type2Parms {};
struct Type3Parms {};
...
struct TypeBase {};
struct Type1 : public TypeBase
{
static bool ValidateParms(const Type1Parms&);
Type1(const Type1Parms&);
};
struct Type2 : public TypeBase
{
static bool ValidateParms(const Type2Parms&);
Type2(const Type2Parms&);
};
struct Type3 : public TypeBase
{
static bool ValidateParms(const Type3Parms&);
Type3(const Type3Parms&);
};
...
struct ValidateParmsVisitor
{
bool operator()(const Type1Parms& parms)
{
return Type1::ValidateParms(parms);
}
bool operator()(const Type2Parms& parms)
{
return Type2::ValidateParms(parms);
}
bool operator()(const Type3Parms& parms)
{
return Type3::ValidateParms(parms);
}
...
};
using TypeParms = std::variant<Type1Parms, Type2Parms, Type3Parms, ...>;
struct CreateObjectVisitor
{
std::unique_ptr<TypeBase> operator()(const Type1Parms& parms)
{
return std::make_unique<Type1>(parms);
}
std::unique_ptr<TypeBase> operator()(const Type2Parms& parms)
{
return std::make_unique<Type2>(parms);
}
std::unique_ptr<TypeBase> operator()(const Type3Parms& parms)
{
return std::make_unique<Type3>(parms);
}
...
};
template<typename TParms>
std::unique_ptr<TypeBase> CreateType(const TParms& parms)
{
unique_ptr<TypeBase> obj;
if (visit(ValidateParmsVisitor{}, parms))
obj = visit(CreateObjectVisitor{}, parms);
return std::move(obj);
}
Is there a way to make this association, especially as a type that can be used with a static member function call?
EDIT: I should explain that this is part of a much larger project, with a number of other design criteria that shape its design.
For example, this is for a client interface, where the API is meant to be as simple as can be expressed. The client only has visibility (via header) to the parms structures and a function that takes the parms & returns an object that contains the objects mentioned above. The original design did indeed have a base structure for the parms, which obviously had to be in the public header. However, this meant that a client could inherit from the base class themselves and pass this into the object creation function, or inherit from the acceptable structures. To avoid segfaults, this necessitated adding runtime checks to be sure the types were acceptable, which was mostly handled by the hash design--although it wasn't quite that simple. When I removed the hash design, I also lost this method of type validation, but I recognized that this would be replaced by a compile time check with the variant<>, handling custom structures (no base to check now). I also learned about the C++ version of the final keyword which handled the inheritance issue.
Additionally, while the code above does not show it, the parms structures contain multiple members and the ValidateParms() functions actually attempt to validate whether the values and combinations are valid.
You can create traits for the association:
template <typename T> struct from_param;
template <> struct from_param<Type1Parms> { using type = Type1; };
template <> struct from_param<Type2Parms> { using type = Type2; };
template <> struct from_param<Type3Parms> { using type = Type3; };
Then, you might do
using TypeParms = std::variant<Type1Parms, Type2Parms, Type3Parms>;
std::unique_ptr<TypeBase> CreateType(const TypeParms& parms)
{
if (std::visit([](const auto& param){
return from_param<std::decay_t<decltype(param)>>::type::ValidateParms(parms);
}, parms))
{
return std::visit([](const auto& param) -> std::unique_ptr<TypeBase> {
return std::make_unique<typename from_param<std::decay_t<decltype(param)>>::type>(parms);
}, parms);
}
return nullptr;
}
Demo
or without variant, if you call with correct type:
template <typename T>
auto CreateType(const T& parms)
{
if (from_param<T>::type::ValidateParms(parms))
{
return std::make_unique<typename from_param<T>::type>(parms);
}
return nullptr;
}
There is a very simple method, a set of overloaded functions:
unique_ptr<TypeBase> CreateType(Type1Params const& params)
{
return make_unique<Type1>(params);
}
unique_ptr<TypeBase> CreateType(Type2Params const& params)
{
return make_unique<Type2>(params);
}
unique_ptr<TypeBase> CreateType(Type3Params const& params)
{
return make_unique<Type3>(params);
}
Notes:
You can add another overload to catch other parameters and then return null, but I think a compile-time error would be preferable.
You could also use a template function and specializations, but there's probably little typing to safe that way.

How to alias a template function inside a template class

I have a need to implement 2 functions inside a templated class, where both functions do similar things, but not everything is the same. My proposed solution was to use if constexpr on a single template function, and then have an alias for each function:
template <typename T>
class MyClass
{
private:
template <bool test>
void TestFunc()
{
if constexpr(test)
{
// Do something
}
else
{
// Do other stuff
}
}
public:
?????? TestTrue = TestFunc<true>;
?????? TestFalse = TestFunc<false>;
}
I'm trying to figure out what should go where the question marks are, so far using, auto and const auto have not worked. I want the user to be able to call TestTrue() and TestFalse() directly from an object of the class directly.
You could do:
void TestTrue() { TestFunc<true>(); }
void TestFalse() { TestFunc<false>(); }
I don't think there's a better way.
For completeness, here's the ugly way.
As mentioned in the comments, TestFunc is a member function, not a type, so if you want to reference an explicit specialization of it, you'll need to use a member function pointer. In our case, these will be pointers of the following type.
using MemberTestFunction = void (MyClass::*)();
We can then acquire pointers to the true and false specialization of TestFunc like so:
template <typename T>
class MyClass
{
// ...
constexpr static MemberTestFunction TestTrue = &MyClass::TestFunc<true>;
constexpr static MemberTestFunction TestFalse = &MyClass::TestFunc<false>;
};
If you're not familiar with pointers to member functions, the syntax for calling TestTrue and TestFalse may look rather bizarre. If you're inside a member function, you can invoke these functions either by using the ->* operator, or by using std::invoke (C++17) from <functional>:
template <typename T>
class MyClass
{
// ...
void foo() {
// Direct call with pointer.
(this->*TestTrue)();
// Call using std::invoke.
std::invoke(TestTrue, this);
}
};
Alternatively, outside of MyClass, these calls would look like the following.
MyClass<nullptr_t> x;
// Using type deducation.
(x.*decltype(x)::TestTrue)();
// Using fully qualified name.
(x.*MyClass<nullptr_t>::TestTrue)();
// Using std::invoke (with type deducation).
std::invoke(decltype(x)::TestTrue, x);
It goes without saying this this is a needlessly obscure way of accomplishing any otherwise simple task. I would not advocate using this technique over creating new functions (as HolyBlackCat suggested) or simply naming TestFunc<true>() and TestFunc<false>() explicitly at the call site.
Transform function TestFunc to functor:
#include <iostream>
template <typename T>
class MyClass
{
private:
template <bool test>
struct TestFunc
{
void operator()() {
if constexpr(test)
{
std::cout << "TestTrue\n";
}
else
{
std::cout << "TestFalse\n";
}
}
};
public:
TestFunc<true> TestTrue;
TestFunc<false> TestFalse;
};
int main()
{
MyClass<int> myClass;
myClass.TestTrue();
myClass.TestFalse();
}

Arbitrary number of parameter assignment with template class

I would like to implement a simple template ObjectPool kind of class.
It has a fixed size vector as a member variable, which elements' are default initialized upon creation.
That's done already and works fine.
The question is what is the best way to assign new elements to the member vector?
I implemented add_old_way member function, which is able to add any type of data to the ObjectPool, and works fine.
My only problem is that i create an extra object which i pass to that function, and inside, i just throw it away after assignment. Waste of resources, if we are talking about giant classes.
How should i implement add_new_way function which is able to take arbitrary parameters and assign them individually to vector element?
Please note ObjectPool is templated so any kind of class can be it's type, not just Bar or Matrix2x2 as in the example below.
I don't eve now the key words here to look it up on the internet, neither if it's possible at all.
Thank you!
template <class T>
class ObjectPool
{
vector<T> mObjects;
ObjectPool() : mObjects(size)
{
}
//that's what i could come up with to add an object to the object pool
void add_old_way(const T& object)
{
...
mObjects[nextFreeIndex] = object;
...
}
//desired way of adding object
void add_new_way(...)
{
mObjects[nextFreeIndex].param1 = param1;
mObjects[nextFreeIndex].param2 = param2;
...
mObjects[nextFreeIndex].paramN = paramN;
}
};
class Bar
{
Bar(int x, string s)
{
mX = x;
mS = s;
}
int mX;
string mS;
};
int main()
{
ObjectPool<Bar> v;
ObjectPool<Matrix2x2> v;
//that's what i could come up with
v.add_old_way(cBar(1,"asdf"));
v.add_old_way(cMatrix2x2(1,2,3,4));
//desired way of adding object
v.add_new_way(1,"asdf");
v.add_new_way(1,2,3,4);
}
Utilize move semantics and perfect forwarding to make the assignment cheap:
template <typename... Args>
void add_new_way(Args&&... args)
{
mObjects[nextFreeIndex] = T{std::forward<Args>(args)...};
}
Live on Coliru
Since the object being assigned from is a temporary, the move-assignment operator of the object being assigned to will be invoked if it exists. This will let the object transfer ownership of any expensive-to-copy resources from the temporary to the object in the pool.
For further reading, take a look at this question: What are move semantics?
If using C++11 and above, to implement your add_new_way method, you can use variadic template parameters and forwarding:
template <typename... Args>
void add_new_way(Args... args) {
mObjects.emplace_back(std::forward<Args>(args)...);
}
Then calling code can do:
v.add_new_way(arg1, arg2, ..., argN);

passing arument list to a constructor through a wrapper in c++

I want to write a wrapper function to which I can pass constructor initialization parameters for some class object. This wrapper will then use this list and pass on to constructor while defining a new object of that class. Not sure what STL or any other data type to use for this purpose. Basically in below code what data type should be used for argument_list. Any library which can assist?
Some code for perspective
class abc{
int a_;
char b_;
public:
abc(int a, char b)
:a_(a), b_(b)
{
}
}
// Wrapper
template <typename T>
void create_object_wrapper(argument_list list) // assuming argument_list can
//hold the parameters as
//intended
{
abc v1(list); //The use case I want
}
int main()
{
create_object_wrapper(1,'a'); // initialize object with (1, 'a')
return 0;
}
Thanks in advance.
use variadic perfect forwarding:
template <typename... Args>
void create_object_wrapper(Args&&... list)
{
abc v1(std::forward<Args>(list)...);
}
Use template parameter packs:
template <typename... Args>
void create_object_wrapper(Args... list)
{
abc v1(list...);
}
This will fail to compile if called with arguments which result in an invalid abc constructor call.
If you want to create the object instantly from the arguments provided, then follows sehe's answer.
If you want to store the arguments provided to create one or more objects later, then you should use a std::tupel. Follow the answers given in this question to see how this can be done. Ideally you want to use C++14 for that (see my answer to that question).

Universal function pointer

There is some class which have methods like:
int getSomething1();
std::string getSomething2();
someClass getSomething3();
There is structure which describes fields of this class like:
{"name of field", pointer to getter, std::type_info}
Then I would like to use it as follows:
if(type == int){
field_int = (int)getter();
}
else if(type == std::string){
field_string = (std::string)getter();
}
etc.
How to transform getters like
int getSomething1();
std::string getSomething2();
etc.
to some universal function pointer and then to get the correct value of field?
This answer of mine to another question addresses your problem pretty well. With some minor modifications, you get this:
template<class C, class T>
T get_attribute(const C& instance, T (C::*func)() const) {
return (instance.*func)();
}
Assuming the following:
struct Foo {
int getSomething1() const;
std::string getSomething2() const;
someClass getSomething3() const;
};
You can use it like this:
Foo foo;
int value = get_attribute<Foo, int>(foo, &Foo::getSomething1);
std::string value = get_attribute<Foo, std::string>(foo, &Foo::getSomething2);
someClass value = get_attribute<Foo, someClass>(foo, &Foo::getSomething3);
You can of course transform get_attribute to a functor to bind some or all of the arguments.
There is no formal universal function pointer, the equivalent of void*
for data. The usual solution is to use void (*)(); you are guaranteed
that you can convert any (non-member) function pointer to this (or any
other function pointer type) and back without loss of information.
If there is a certain similarity in the function signatures (e.g. all
are getters, with no arguments) and how they are used, it may be
possible to handle this with an abstract base class and a set of derived
classes (possibly templated); putting pointers to instances of these
classes in a map would definitely be more elegant than an enormous
switch.
What you are trying to achieve can be better achieved with already existing containers such as a boost fusion sequence. I'd advice that you try this first.
Templates to the rescue!
// Create mapping of type to specific function
template <typename T> T getSomething(); // No default implementation
template <> int getSomething<int>() { return getSomething1(); }
template <> std::string getSomething<std::string>() { return getSomething2(); }
template <> someClass getSomething<someClass>() { return getSomething3(); }
// Convenience wrapper
template <typename T> void getSomething(T& t) { t = getSomething<T>(); }
// Use
int i = getSomething<int>();
std::string s;
getSomething(s);
As I understand, your difficulty is in storing the function pointers, since they are of different types. You can solve this using Boost.Any and Boost.Function.
#include <boost/any.hpp>
#include <boost/function.hpp>
int getInt() {
return 0;
}
std::string getString() {
return "hello";
}
int main()
{
boost::function<boost::any ()> intFunc(getInt);
boost::function<boost::any ()> strFunc(getString);
int i = boost::any_cast<int>(intFunc());
std::string str = boost::any_cast<std::string>(strFunc());
}