Error with lambda in template member function - c++11

I have the following c++ code
#include <iostream>
template<typename Func>
class Foo
{
private:
Func func;
public:
Foo(Func func) : func(func) {}
template<typename T>
Func wrap()
{
Func clbk = func;
auto wrapperCB = [clbk](T t) {
auto job = [clbk, t](){
clbk(t);
};
job();
};
return wrapperCB;
}
template<typename T>
void call(T t)
{
func(t);
}
};
int main()
{
int m = 2;
auto f = [](int & p) {std::cout << "test success " << p << "\n";};
auto obj = std::make_shared<Foo<std::function<void(int &)>>>(f);
auto wrapper = obj->template wrap<int &>();
wrapper(m);
return 0;
}
This is giving compilation error
tsavs-mbp:p utsagarw$ clear; g++ -std=c++11 a.cpp -o z; ./z
a.cpp:18:17: error: no matching function for call to object of type 'const std::__1::function<void (int &)>'
clbk(t);
^~~~
a.cpp:38:32: note: in instantiation of function template specialization 'Foo<std::__1::function<void (int &)> >::wrap<int &>' requested here
auto wrapper = obj->template wrap<int &>();
^
/Applications/Xcode_10.1/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/functional:1677:9: note: candidate function not viable: 1st argument ('const int') would lose const qualifier
_Rp operator()(_ArgTypes...) const;
^
1 error generated.
I don't understand this error. Where did this const come from?
It is building successfully if in wrap I don't create job functor and call clbk directly. What is this job doing to type T?
template<typename T>
Func wrap()
{
Func clbk = func;
auto wrapperCB = [clbk](T t) {
clbk(t);
};
return wrapperCB;
}

If you want to modify any captured variable inside lambda you have to specify it as mutable.
t variable is captured by copy, so you can only read it:
auto job = [clbk, t]() // <-- t passed by copy
{
clbk(t); // clbk takes t by reference -> int&
};
your callback, clbk has signature int& so it means it could modify t. What is not allowed.
Solution:
auto job = [clbk, t]() mutable // keyword 'mutable' added
{
clbk(t); // clbk can change t
};
or make function taking const int& as parameter - then t can be only read.
Demo

Related

Template function parameter T deduction misunderstanding

I am very new to C++ and I was trying out templating to understand how it works. I have a template function that accepts an argument of type T. The problem that I am facing is that T's type is determined at runtime depending on the value of T and the compiler throws an error because it determines the type without considering the if-else-if-else block.
#include <iostream>
using namespace std;
class MyClass {
public:
void setInt(int x) {}
void setString(string y) {} // copy string object
};
void f1() {
cout << "break" << endl;
}
template<typename T, typename... Args>
void f1(T arg, Args... args) {
string _type(typeid(arg).name());
cout << __PRETTY_FUNCTION__ << endl;
cout << _type << endl;
MyClass c1;
if( _type.compare("i") == 0 ) {
c1.setInt(arg);
} else if ( _type.compare("PKc") == 0 ) {
//c1.setString(arg);
}
f1(args...);
};
int main() {
f1(7, 3.3, "asd", 0xa1);
return 0;
}
The output:
prog.cpp: In instantiation of ‘void f1(T, Args ...) [with T = const char*; Args = {int}]’:
prog.cpp:30:4: recursively required from ‘void f1(T, Args ...) [with T = double; Args = {const char*, int}]’
prog.cpp:30:4: required from ‘void f1(T, Args ...) [with T = int; Args = {double, const char*, int}]’
prog.cpp:35:24: required from here
prog.cpp:25:13: error: invalid conversion from ‘const char*’ to ‘int’ [-fpermissive]
c1.setInt(arg);
^~~
prog.cpp:6:19: note: initializing argument 1 of ‘void MyClass::setInt(int)’
void setInt(int x) {}
~~~~^
https://ideone.com/the4AP (The link to online compiler)
One possible approach:
void f1_helper(int arg, MyClass* c) {
c->setInt(arg);
}
void f1_helper(string arg, MyClass* c) {
c->setString(arg);
}
template<typename... Args>
void f1(Args... args) {
MyClass c1;
auto _ = {(f1_helper(args, &c1), 0) ...};
}
Demo

Filling N sized array with constexpr c++11

I'm aware that my code should work in c++14 but I have to replicate this behaviour in c++11, I havent been able to make a equivalent init() could anyone help?
enum MyEnum {
BANANA, APPLE, PINEAPPLE, ORANGE, FRUIT_AMOUNT
};
template<MyEnum>
struct Fruit{
virtual ~Fruit(){}
virtual void dostuff(){}
};
template <>
struct Fruit<ORANGE>{
void dostuff(){ cout<<"Hey apple!"<<endl;
}
constexpr array< Fruit*, FRUIT_AMOUNT > init(){
array< Fruit*, FRUIT_AMOUNT > myArray;
for(int i =0; i < FRUIT_AMOUNT; i++)
myArray[i] = new Fruit< (MyEnum) i >();
return myArray;
}
array<Fruit*, FRUIT_AMOUNT> myPrettyFruits = init();
I'm aware that my code should work in c++14
Ehmm... not at all.
Your code has a lot of problems.
Some of them, in no particular order
(1) You can't write
array<Fruit*, FRUIT_AMOUNT>
because Fruit isn't a type; it's a template class.
So, by example, Fruit<BANANA> is a type, and you can write
std::array<Fruit<BANANA> *, FRUIT_AMOUNT>
but you can't have a pointer to Fruit because Fruit (without explicating a template argument) isn't a type.
A possible solution for this problem is make all Fruit types inheriting from a common base; by example
struct FruitBase
{ };
template <MyEnum>
struct Fruit : public FruitBase
{
virtual ~Fruit () {}
virtual void dostuff () {}
};
This way you can have an array of FruitBase pointers
std::array<FruitBase *, FRUIT_AMOUNT>
So you can put in the array pointer to Fruit<Something> types that are also FruitBase pointers.
(2) You can't have
Fruit< (MyEnum) i >
where i is a run-time known variable, because a template argument must be known compile time.
A possible C++14 solution is use std::make_index_sequence to get a sequence of template (so compile-time known) std::size_t values.
I suggest something as follows
template <std::size_t ... Is>
constexpr std::array<FruitBase *, FRUIT_AMOUNT>
init_helper (std::index_sequence<Is...> const &)
{ return { { (FruitBase*)(new Fruit<(MyEnum)Is>()) ... } }; }
constexpr std::array<FruitBase *, FRUIT_AMOUNT> init ()
{ return init_helper(std::make_index_sequence<FRUIT_AMOUNT>{}); }
Observe that both functions are a single return statement; and this is necessary for a constexpr C++11 function.
Unfortunately std::index_sequence and std::make_index_sequence are available only starting from C++14. But it's possible make a C++11 substitute for they.
(3) new Something{} can't be executed compile-time
So you can define init_helper() constexpr but it's a fake constexpr function (so also init() is a fake constexpr function) because can't be executed compile time.
So you can write
std::array<FruitBase *, FRUIT_AMOUNT> myPrettyFruits = init();
but myPrettyFruits is initialized run-time.
If you try to initialize it compile-time
constexpr std::array<FruitBase *, FRUIT_AMOUNT> myPrettyFruits = init();
you get a compilation error.
The following is a full compiling C++11 example, with a std::index_sequence/std::make_index_sequence replacement, that works only run-time
#include <array>
#include <iostream>
template <std::size_t...>
struct indexSequence
{ using type = indexSequence; };
template <typename, typename>
struct concatSequences;
template <std::size_t... S1, std::size_t... S2>
struct concatSequences<indexSequence<S1...>, indexSequence<S2...>>
: public indexSequence<S1..., ( sizeof...(S1) + S2 )...>
{ };
template <std::size_t N>
struct makeIndexSequenceH
: public concatSequences<
typename makeIndexSequenceH<(N>>1)>::type,
typename makeIndexSequenceH<N-(N>>1)>::type>::type
{ };
template<>
struct makeIndexSequenceH<0> : public indexSequence<>
{ };
template<>
struct makeIndexSequenceH<1> : public indexSequence<0>
{ };
template <std::size_t N>
using makeIndexSequence = typename makeIndexSequenceH<N>::type;
enum MyEnum
{ BANANA, APPLE, PINEAPPLE, ORANGE, FRUIT_AMOUNT };
struct FruitBase
{ };
template <MyEnum>
struct Fruit : public FruitBase
{
virtual ~Fruit () {}
virtual void dostuff () {}
};
template <>
struct Fruit<ORANGE> : public FruitBase
{ void dostuff () { std::cout << "Hey apple!" << std::endl; } };
// fake constexpr function: new can't be executed compile-time
template <std::size_t ... Is>
constexpr std::array<FruitBase *, FRUIT_AMOUNT>
init_helper (indexSequence<Is...> const &)
{ return { { (FruitBase*)(new Fruit<(MyEnum)Is>()) ... } }; }
// fake constexpr: init_helper() can't be executed compile-time
constexpr std::array<FruitBase *, FRUIT_AMOUNT> init ()
{ return init_helper(makeIndexSequence<FRUIT_AMOUNT>{}); }
int main ()
{
// compile (executed run-time)
std::array<FruitBase *, FRUIT_AMOUNT> myPrettyFruits = init();
// compilation error (init() can't be executed compile-time)
//constexpr std::array<FruitBase *, FRUIT_AMOUNT> myPrettyFruits = init();
}

Type mismatch of pointer to template member function

I am following this code snippet which makes it easier to pass a member function to an interface expecting a C-style callback (that is, the interface expects a function pointer to the callback, and a void* pointer to user data which will in turn be passed to the callback). Effectively I want to convert Helper::M to Helper::V below.
I am trying to modify the snippet to automatically deduce the template parameters. Here is my current attempt.
#include <iostream>
template <typename R, typename T, typename... Args>
struct Helper {
using V = R (*)(void*, Args...);
using M = R (T::*)(Args...);
template <M m>
static R Fn(void* data, Args... args) {
return (static_cast<T*>(data)->*m)(std::forward<Args...>(args...));
}
};
template <typename R, typename T, typename... Args>
typename Helper<R, T, Args...>::V Cast(R (T::*m)(Args...)) {
return Helper<R, T, Args...>::template Fn<m>;
}
int CIntf(void* data, int (*f)(void*, int)) { return f(data, 1); }
struct UserData {
int x;
int Add(int y) { return x + y; }
};
int main(int argv, char** argc) {
UserData data = {4};
// Explicit parameters; works.
std::cout << CIntf(&data, Helper<int, UserData, int>::Fn<&UserData::Add>)
<< "\n";
// Deduced parameters; fails.
std::cout << CIntf(&data, Cast(&UserData::Add)) << "\n";
return 0;
}
I tried to compile with gcc -std=c++11 -lstdc++. The explicit parameters method works fine, but the deduced parameters method gives the following error:
tmp.cc: In instantiation of ‘typename Helper<R, T, Args>::V Cast(R (T::*)(Args ...)) [with R = int; T = UserData; Args = {int}; typename Helper<R, T, Args>::V = int (*)(void*, int)]’:
tmp.cc:30:58: required from here
tmp.cc:15:42: error: no matches converting function ‘Fn’ to type ‘using V = int (*)(void*, int) {aka int (*)(void*, int)}’
return Helper<R, T, Args...>::template Fn<m>;
^~~~~
tmp.cc:8:12: note: candidate is: template<int (UserData::* m)(int)> static R Helper<R, T, Args>::Fn(void*, Args ...) [with R (T::* m)(Args ...) = m; R = int; T = UserData; Args = {int}]
static R Fn(void* data, Args... args) {
Note that it correctly deduced the template parameters, but failed to convert Helper<int, UserData, int>::Fn<m> to int (*)(void*, int); why? This same conversion succeeded in the explicit case (unless m is somehow different from &UserData::Add).
Unfortunately you'll have to use a macro for this:
#define makeFunc(method) &Helper<decltype(method)>::Fn<method>
And redefine your helper like this for it to work:
template <typename T>
struct Helper;
template <typename R, typename T, typename... Args>
struct Helper<R(T::*)(Args...)>
The reason why you can't use deduction for this, is that deduction only works on function arguments which are run-time values. And you need to use a method's address as template argument which should be a compile-time value.
So when you do this:
return Helper<R, T, Args...>::template Fn<m>;
you are passing a run-time value m as a template argument which is impossible.
For reference, here is the complete code using the macro. Also note the use of std::forward in the original code was incorrect for multiple arguments (see this answer).
#include <iostream>
#include <utility>
template <typename T>
struct Helper;
template <typename R, typename T, typename... Args>
struct Helper<R (T::*)(Args...)> {
template <R (T::*m)(Args...)>
static R Fn(void* t, Args... args) {
return (static_cast<T*>(t)->*m)(std::forward<Args>(args)...);
}
};
#define VOID_CAST(m) &Helper<decltype(m)>::Fn<m>
struct UserData {
int x;
int Add1(int y) { return x + y; }
int Add2(int y, int z) { return x + y + z; }
};
int Call1(void* data, int (*f)(void*, int)) { return (*f)(data, 1); }
int Call2(void* data, int (*f)(void*, int, int)) { return (*f)(data, 1, 2); }
int main() {
UserData data = {4};
std::cout << Call1(&data, VOID_CAST(&UserData::Add1)) << "\n";
std::cout << Call2(&data, VOID_CAST(&UserData::Add2)) << "\n";
return 0;
}

Create c-wrappers for c++ objects with default instance and deduce prototypes

I have a number of C++ structs with a number of methods. The C++ structs have a
"default" instance, and I would like to expose a "c" wrapper functions that uses
this default instance. But I would also like to avoid repeating all the
prototyles.
Alkind of C++11/14/17 and/or macro tricks are welcome, but I do not want to use
code-generators.
I have something that almost works, but I'm still struggling with a few
details.
// C++ class that have a "default-instance" ///////////////////////////////////
struct Foo {
int a() { return 1; }
int b(int) { return 2; }
int c(int, int) { return 3; }
};
Foo *FOO = nullptr;
// emulating existing c code that can not be changed //////////////////////////
typedef int (*ptr_a_t)();
ptr_a_t ptr_a = nullptr;
typedef int (*ptr_b_t)(int);
ptr_b_t ptr_b = nullptr;
typedef int (*ptr_c_t)(int, int);
ptr_c_t ptr_c = nullptr;
// Wrapper code (almost generic) //////////////////////////////////////////////
template <typename T, T>
struct Proxy;
// Wrapper class that will use the defualt instance if initialized (FOO is
// hardcoded).
template <typename T, typename R, typename... Args, R (T::*mf)(Args...)>
struct Proxy<R (T::*)(Args...), mf> {
static R call(Args... args) {
if (FOO) {
// ^^^
return ((*FOO).*mf)(args...);
// HARD-CODED ^^^^
} else {
return -1;
}
}
};
// Helper function to deduce the Proxy-class (method 'b' is hardcoded)
template <typename T, typename R, typename... Args>
auto deduce_args(R (T::*mf)(Args...)) -> Proxy<R (T::*)(Args...), &T::b> {
// HARD-CODED ^
return Proxy<R (T::*)(Args...), &T::b>();
// HARD-CODED ^
}
// Wrap the methods ////////////////////////////////////////////////////////
//#define wrap_a decltype(deduce_args(&Foo::a))::call
#define wrap_b decltype(deduce_args(&Foo::b))::call
//#define wrap_c decltype(deduce_args(&Foo::c))::call
int main() {
// Test that it works
//ptr_a = &wrap_a; // does not work due to hard-coded method
ptr_b = &wrap_b;
//ptr_c = &wrap_c; // does not work due to hard-coded method
return ptr_b(0);
}
I can live with the hard-coded "FOO" in the proxy, as I only need one proxy per class, but it would be cool if the instance pointer could be passed as a
template argument.
The hard-coded method in "deduce_args" is really anoying, how can I eliminate
that??
Is there a better way to do this (the function pointers can not be replaced with std::function).
Using C++14 alias turned out to be a much easier way of achieving what I wanted.
// compile using the "-std=c++14" flag
// C++ class that have a "default-instance" ///////////////////////////////////
struct Foo {
int a() { return 1; }
int b(int) { return 2; }
int c(int, int) { return 3; }
};
Foo *FOO = nullptr;
// emulating existing c code that can not be changed //////////////////////////
typedef int (*ptr_a_t)();
ptr_a_t ptr_a = nullptr;
typedef int (*ptr_b_t)(int);
ptr_b_t ptr_b = nullptr;
typedef int (*ptr_c_t)(int, int);
ptr_c_t ptr_c = nullptr;
// Wrapper code ///////////////////////////////////////////////////////////////
template <typename T, T, typename P, P>
struct Proxy;
template <typename T, typename R, typename... Args, R (T::*mf)(Args...),
typename P, P p>
struct Proxy<R (T::*)(Args...), mf, P, p> {
static R call(Args... args) {
if (*p) {
return ((*(*p)).*mf)(args...);
} else {
return -1;
}
}
};
// Wrap the methods ///////////////////////////////////////////////////////////
#define WRAP(n, obj, m, ptr) \
const auto &n = Proxy<decltype(&obj::m), &obj::m, obj **, &ptr>::call
WRAP(wrap_a, Foo, a, FOO);
WRAP(wrap_b, Foo, b, FOO);
WRAP(wrap_c, Foo, c, FOO);
int main() {
// Test that it works
ptr_a = &wrap_a;
ptr_b = &wrap_b;
ptr_c = &wrap_c;
return ptr_b(0);
}

C++1y return type inference

Programming languages with some variant of Hindley-Milner type inference can easily infer the type of expressions such as
let rec fix f x = f (fix f) x
whereas the return type inference in C++1y fails for the following :
int main() {
auto fix =
[&](auto f) {
return [&](auto x) {
return f(fix(f))(x);
};
};
return 0;
}
I tried this with clang 3.5 and the command
clang++ -std=c++1y fix.cc
and I get
fix.cc:7:18: error: variable 'fix' declared with 'auto' type cannot appear in its
own initializer
return f(fix(f))(x);
What is lacking in C++'s return type inference that disallows using variable in it's own initializer when return type has to be inferred? What can I do to work around this problem, and better still, what can we do to fix this in the language?
The issue here isn't a problem with type inference. The immediate issue is simply that no syntax exists for a lambda to refer to itself even though doing so is perfectly reasonable and implementable. Function objects can easily be defined that refer to themselves.
Your particular lambda has a problem in that you are capturing a local object by reference and then allowing that reference to escape the scope where it's valid, but if we change it to:
int main() {
auto fix =
[](auto &f) {
return [&](auto x) {
return f(fix(f))(x);
};
};
return 0;
}
Then we can write and use equivalent function objects:
#include <iostream>
struct lambda1 {
template<typename F>
auto operator() (F &f) const;
};
template<typename F>
struct lambda2 {
lambda1 const &l1;
F &f;
lambda2(lambda1 const &l1, F &f) : l1(l1), f(f) {}
template<typename X>
auto operator() (X x) const { return f( l1(f))(x); }
};
template<typename F>
auto lambda1::operator() (F &f) const {
return lambda2<F>(*this, f);
} // ^
// |
// --- there's no syntax to do this inside a lambda.
int main() {
lambda1 fix;
auto f = [](auto&&){ return [](int x) {return x;}; };
std::cout << fix(f)(5) << '\n';
}
If some kind of syntax were added to allow a lambda to refer to itself then this would be fine.

Resources