Perfect forwarding to base class - c++

#include <utility>
#include <vector>
#include <cstdint>
template <typename T>
struct Base
{
protected:
Base(T&& data):
data(std::forward(data)){
}
virtual ~Base(){};
public:
T getData() const {return data;}
void setData(T&& data) {
this->data = std::forward(data);
}
private:
T data;
};
struct DerivedA: public Base<int>
{
public:
DerivedA(int data):
Base(std::move(data)){//Should I use std::forward instead of std::move here?
}
};
struct DerivedB: public Base<const std::vector<uint16_t> >
{
public:
DerivedB(const std::vector<uint16_t>& data):
Base(std::move(data)){
}
};
My requirements is to have 0 copying of objects when creating the Derived Classes above.
But no matter how I write the above I get compiler errors, these are the latest:
bin/Base.h: In instantiation of ‘Base<T>::Base(int, int, int, T&&) [with T = int]’:
bin/Base.h:33:82: required from here
bin/Base.h:12:96: error: no matching function for call to ‘forward(int&)’
/usr/include/c++/4.7/bits/move.h:77:5: note: template<class _Tp> constexpr _Tp&& std::forward(typename std::remove_reference<_Tp>::type&)
/usr/include/c++/4.7/bits/move.h:77:5: note: template argument deduction/substitution
What am I doing wrong here?
Also, should I do std::move(data) when data in an int or std::forward?

If you want to perfectly forward an argument, you'll either need to provide the corresponding three overloads or make the argument a template argument. In either case, when you want to use std::forward() you'll need to specify the first template argument as it is not deduced. Most likely, you'd use something like this:
template <typename T>
class Base {
public:
template <typename A>
Base(A&& data): data(std::forward<A>(data)) {}
};
Trying to std::move(data) when data is a std::vector<uint16_t> const& won't move the vector nor will it make the object look like a non-const rvalue: if you want to make the vector movable, you'll need to pass it as non-const reference, an rvalue, or a value. You may also want to deduce the type or overload on std::vector<uint16_t>&& and std::vector<uint16_t>&. For both of these overloads using std::move() does the trick. If you deduce the type, you'll use std::forward<A>(data) again. In case the deduced type looks to scare, you can constrain it using std::enable_if<...> using something like this:
template <typename A>
DerivedB(A&& arg,
typename std::enable_if<std::is_same<typename std::decay<A>::value,
std::vector<uint16_t>>::value>::type* = 0):
Base(std::forward<A>(arg)) {
}

Related

Passing unique_ptr<Derived<T>> to a function

I need to pass a unique pointer to a derived template class to a function that takes a unique base template class, like this:
template <typename T>
class Base {};
template <typename T>
class Derived : public Base<T> {};
template <typename T>
void foo(std::unique_ptr<Base<T>>){}
//or
template <typename T>
class MyClass{
public:
MyClass(std::unique_ptr<Base<T>> arg) : _arg(std::move(arg)) {}
private:
std::unique_ptr<Base<T>> _arg;
};
int main()
{
auto b = make_unique<Derived<int>>();
foo(std::move(b));
MyClass mc(std::move(b))
}
Why is this not working and how can I fix it?
I get an error:
'void foo1<T>(std::unique_ptr<Base<T>,std::default_delete<Base<T>>>)': cannot convert argument 1 from 'std::unique_ptr<Derived<int>,std::default_delete<Derived<int>>>' to 'std::unique_ptr<Base<T>,std::default_delete<Base<T>>>'
but it work
auto derived = std::make_unique<Derived<int>>();
std::unique_ptr<Base<int>> base = std::move(derived);
C++ doesn't deduce template arguments in this situation. You can specify <int>, and that will succeed.
foo<int>(std::move(b)); // fine
MyClass<int> mc(std::move(b)); // fine
See it on coliru
You can't have template argument deduction also consider implicit conversions, at least not in most situations. Normally the argument type must match the parameter type exactly for deduction of a template argument to be possible (in this case to deduce T), but std::unique_ptr<Base<int>> and std::unique_ptr<Dervived<int>> are not the same type.
As the other answer suggests you can explicitly specify the template argument instead of trying to have it be deduced.
If you want to automate this without having to add anything to Derived or Base you can however make use of one of the exceptions to the general rule above. If the template parameter is a reference-to or pointer-to base of the argument type, then it may (with certain conditions) still be used for deduction:
// Here an exception to the deduction rules applies
// and `Base<T>*` can be deduced against a pointer `X*`
// if `X` is (uniquely) derived from a `Base<T>`
template<typename T>
auto as_base_ptr(Base<T>* p){
return p;
}
template<typename X>
auto to_base_unique_ptr(std::unique_ptr<X> p) {
using base_type = std::remove_pointer_t<decltype(as_base_ptr(std::declval<X*>()))>;
return std::unique_ptr<base_type>(std::move(p));
}
template <typename T>
void foo(std::unique_ptr<Base<T>>){
}
template <typename X>
void foo(std::unique_ptr<X> p){
foo(to_base_unqiue_ptr(std::move(p)));
}
But even simpler you can ask yourself whether you really need to have the function foo take std::unique_ptr<Base<T>> specifically (e.g. because you need access to T) or whether std::unique_ptr<X> wouldn't already be enough.

avoid pointer-to-member-function for non-class type

I am writing a kind of container class, for which I would like to offer an apply method which evaluates a function on the content of the container.
template<typename T>
struct Foo
{
T val;
/** apply a free function */
template<typename U> Foo<U> apply(U(*fun)(const T&))
{
return Foo<U>(fun(val));
}
/** apply a member function */
template<typename U> Foo<U> apply(U (T::*fun)() const)
{
return Foo<U>((val.*fun)());
}
};
struct Bar{};
template class Foo<Bar>; // this compiles
//template class Foo<int>; // this produces an error
The last line yields error: creating pointer to member function of non-class type ‘const int’. Even though I only instantiated Foo and not used apply at all. So my question is: How can I effectively remove the second overload whenever T is a non-class type?
Note: I also tried having only one overload taking a std::function<U(const T&)>. This kinda works, because both function-pointers and member-function-pointers can be converted to std::function, but this approach effectively disables template deduction for U which makes user-code less readable.
Using std::invoke instead helps, it is much easier to implement and read
template<typename T>
struct Foo
{
T val;
template<typename U> auto apply(U&& fun)
{
return Foo<std::invoke_result_t<U, T>>{std::invoke(std::forward<U>(fun), val)};
}
};
struct Bar{};
template class Foo<Bar>;
template class Foo<int>;
However, this won't compile if the functions are overloaded
int f();
double f(const Bar&);
Foo<Bar>{}.apply(f); // Doesn't compile
The way around that is to use functors instead
Foo<Bar>{}.apply([](auto&& bar) -> decltype(auto) { return f(decltype(bar)(bar)); });
Which also makes it more consistent with member function calls
Foo<Bar>{}.apply([](auto&& bar) -> decltype(auto) { return decltype(bar)(bar).f(); });
In order to remove the second overload you'd need to make it a template and let SFINAE work, e. g. like this:
template<typename T>
struct Foo
{
T val;
//...
/** apply a member function */
template<typename U, typename ObjT>
Foo<U> apply(U (ObjT::*fun)() const)
{
return Foo<U>((val.*fun)());
}
};
Alternatively, you could remove the second overload altogether, and use lambda or std::bind:
#include <functional> // for std::bind
template<typename T>
struct Foo
{
T val;
/** apply a member function */
template<typename U, typename FuncT>
Foo<U> apply(FuncT&& f)
{
return {f(val)};
}
};
struct SomeType
{
int getFive() { return 5; }
};
int main()
{
Foo<SomeType> obj;
obj.apply<int>(std::bind(&SomeType::getFive, std::placeholders::_1));
obj.apply<int>([](SomeType& obj) { return obj.getFive(); });
}
How can I effectively remove the second overload whenever T is a non-class type?
If you can use at least C++11 (and if you tried std::function I suppose you can use it), you can use SFINAE with std::enable_if
template <typename U, typename V>
typename std::enable_if<std::is_class<V>{}
&& std::is_same<V, T>{}, Foo<U>>::type
apply(U (V::*fun)() const)
{ return Foo<U>((val.*fun)()); }
to impose that T is a class.
Observe that you can't check directly T, that is a template parameter of the class, but you have to pass through a V type, a template type of the specific method.
But you can also impose that T and V are the same type (&& std::is_same<V, T>{}).

Cannot bind lvalue to A<Cv2>&&

I thought universal reference (T&&) is supposed to take any kind of reference. But the following doesn't work.
I run into this problem when I try to be const-correct in a library that I am writing. I am new to C++ and haven't seen something like this before.
test.cpp:
enum Cv_qualifier {
constant,
non_const
};
template <Cv_qualifier Cv> class A;
template<>
class A<Cv_qualifier::constant> {
public:
template<Cv_qualifier Cv2>
void t(const A<Cv2>&& out) {}
};
template <>
class A<Cv_qualifier::non_const> {
public:
template<Cv_qualifier Cv2>
void t(const A<Cv2>&& out) {}
};
int main()
{
A<Cv_qualifier::non_const> a;
A<Cv_qualifier::constant> b;
a.t(b);
}
Error (compiled with g++ test.cpp -std=c++11):
test.cpp: In function ‘int main()’:
test.cpp:24:10: error: cannot bind ‘A<(Cv_qualifier)0u>’ lvalue to ‘const A<(Cv_qualifier)0u>&&’
a.t(b);
^
test.cpp:17:10: note: initializing argument 1 of ‘void A<(Cv_qualifier)1u>::t(const A<Cv2>&&) [with Cv_qualifier Cv2 = (Cv_qualifier)0u]’
void t(const A<Cv2>&& out) {}
^
By the way, in the actual program, the class A does not own any actual data, and contain references to another class that actually hold the data. I hope this means I am not constantly create indirection/copy data when I allow the member function t of class A to accept temporary objects.
Universal reference, or forwarding reference, only happen because of reference collapsing. It work that way:
T&& & -> T&
T& && -> T&
T&& && -> T&&
That way, when you receive T&& in a template function, the rvalue reference can collapse to other types of reference depending of the type of T. In any other cases, when the collapsing don't happen, SomeType&& will stay SomeType&& and will be an rvalue reference.
With that said, if you want your function to support forwarding, you can do that:
template <Cv_qualifier Cv> struct A;
template<>
struct A<Cv_qualifier::constant> {
template<typename T>
void t(T&& out) {}
};
template <>
struct A<Cv_qualifier::non_const> {
template<typename T>
void t(T&& out) {}
};
Indeed, now the collapsing happen. If you want to extract the Cv_qualifier value from T, you can make yourself a type trait that do that:
template<typename>
struct CvValue;
template<Cv_qualifier cv>
struct CvValue<A<cv>> {
constexpr static Cv_qualifier value = cv;
};
Then, inside your function t, you can do that:
// v----- This is a good practice to apply a constraint
template<typename T, std::void_t<decltype(CvValue<std::decay_t<T>>::value)>* = 0>
auto t(T&& out) {
constexpr auto cv = CvValue<std::decay_t<T>>::value;
// do whatever you want with cv
}
If you can't use C++17's std::void_t, you can implement it like that:
template<typename...>
using void_t = void;
However, if you only want to test if T is an A<...>, use this:
template<typename>
struct is_A : std::false_type {};
template<Cv_qualifier cv>
struct is_A<A<cv>> : std::true_type {};
Don't forget, to use it with std::decay_t:
template<typename T, std::enable_if_t<std::is_A<std::decay_t<T>>::value>* = 0>
void t(T&& out) {}

c++ parameter pack specification in constructor rather than template

Unlike function declarations with parameter packs, I've found that classes require the type for each argument in the angle brackets...
Component<IntegerPair, int, int> temp(40, 5);
...which seems redundant. Here's how I defined Component:
template<typename T, class... T_Args>
class Component
{
public:
Component(T_Args... args)
: m_data(args...)
{}
T m_data;
};
Is there a way to remove int, int from the above statement?
If so, is it ok to remove it?
Also, is my way of instantiation m_data safe? When using
std::forward<T_Args>(args)... my compiler told me I didn't have a
constructor that could convert all of the argument types.
One way is to make the constructor a template:
#include <utility>
struct IntegerPair {
IntegerPair(int, int) {}
};
template<typename T>
class Component
{
public:
template<typename... T_Args>
Component(T_Args&&... args)
: m_data(std::forward<T_Args>(args)...)
{}
T m_data;
};
int main()
{
Component<IntegerPair> c {1,2};
}
This is functionally equivalent to std::vector and its member function emplace_back. It's perfectly ok, IMO. The error messages are pretty cryptic, as usual in template constructs like this, but this can be mitigated with an appropriate static_assert.
template parameter deduction only work for function calls so the basic pattern to achieve what you want looks like this:
template<typename T, class... T_Args>
Component<T, T_Args...> makeComponent(T_Args&&... args) {
return Component<T, T_Args...>(std::forward<T_Args>(args)...);
}
Usage:
auto c = makeComponent<IntegerPair>(1, 1)

C++: Function pointer as Template argument instead of functor

I have been trying to create this class which can either use the default functor as an argument or the user can provide one if he wants. But I am unable to pass function pointer as my template argument. Can you please help me in understanding what I am missing.
template <typename T>
struct CheckFunctor
{
bool operator()(T obj)
{
return true;
}
};
template <typename _Ty,
class _Pr = CheckFunctor<_Ty>
>
class MyClass
{
typedef _Ty mapped_type;
typedef _Pr CanBeCleaned_type;
_Ty data;
CanBeCleaned_type predicate;
public:
void SomeMethod()
{
if( predicate(data))
{
std::cout << "Do something";
}
}
MyClass(_Ty timeOutDuration, _Pr pred = _Pr())
: data( timeOutDuration), predicate( pred)
{}
};
template< typename T>
struct CheckEvenFunctor
{
bool operator()(T val)
{
return (val%2 == 0);
}
};
bool CheckEven( int val)
{
return (val%2 == 0);
}
int main()
{
//Usage -1
MyClass<int> obj1( 5);
//Usage- 2
MyClass< int, CheckEven> obj2(6, CheckEven); //Error: 'CheckEven' is not a valid template type argument for parameter '_Pr'
//Usage -3
MyClass<int, CheckEvenFunctor<int>>( 7);
}
You are trying to pass CheckEven as a type parameter even though CheckEven is not a type but a function (of type bool(int)). You should define the type as a pointer to the type of function that you are passing. decltype is handy here:
MyClass< int, decltype(&CheckEven)> obj2(6, CheckEven);
You can also create a factory function and let the compiler deduce the template parameters:
template<class T, class F>
MyClass<T, F> makeClass(T timeOutDuration, F pred) {
return {timeOutDuration, pred};
}
auto obj2 = makeClass(6, CheckEven);
It is possible to allow a template parameter to be a function, but your MyClass is expecting the second argument to be a type, not a function. You might think that template specialization could be used to allow you to define MyClass to also take a function in the second template argument, but it won't work. Template specialization allows you to modify the behavior of MyClass for particular types that are passed as template arguments, but those types must still match the template definition of MyClass, which is that the two arguments are types.
You could modify the type of MyClass you are instantiating by making the second argument a function pointer type as suggested in a different answer, but you would lose the advantage of template expansion making your calls to functions inline. Another solution would be to create a helper class that will convert your function into a functor, and use that to create your MyClass instance.
template <bool P (int)>
struct F {
bool operator()(int obj) { return P(obj); }
};
//...
MyClass<int, F<CheckEven> > obj2(6);