arobenko / embxx

embxx - Embedded C++ Library
GNU General Public License v3.0
262 stars 35 forks source link

StaticFunction #19

Closed user706 closed 6 years ago

user706 commented 6 years ago

Hi,

the following code will fail to compile. (It only compiles, if I uncomment the const.)

#include <iostream>

#include "StaticFunction.h"

struct Func1
{
    int operator()(int val) // const
    {
        return val * 2;
    }

    int a[10];
};

struct Func2
{
    int operator()(int val) // const
    {
        return val * a;
    }

    int& a;
};

typedef embxx::util::StaticFunction<int(int), 48> embxx_util_StaticFunction;

int main()
{
    embxx::util::StaticFunction<int(int), sizeof(Func1{})> f1(Func1{});
    std::cout << f1(3) << std::endl;

    int a = 4;
    embxx::util::StaticFunction<int(int), sizeof(Func2{a})> f2(Func2{a});
    std::cout << f2(3) << std::endl;
    return 0;
}

Compilation error is

In file included from main.cpp:3:0:
StaticFunction.h: In instantiation of ‘TRet embxx::util::StaticFunction<TRet(TArgs ...), TSize>::InvokerBound<TBound>::exec(TArgs ...) const [with TBound = Func2; long unsigned int TSize = 8ul; TRet = int; TArgs = {int}]’:
main.cpp:37:1:   required from here
StaticFunction.h:361:46: error: no match for call to ‘(const Func2) (int)’
     return func_(std::forward<TArgs>(args)...);
                                              ^
main.cpp:17:9: note: candidate: int Func2::operator()(int) <near match>
     int operator()(int val)
         ^~~~~~~~
main.cpp:17:9: note:   passing ‘const Func2*’ as ‘this’ argument discards qualifiers
In file included from main.cpp:3:0:
StaticFunction.h: In instantiation of ‘TRet embxx::util::StaticFunction<TRet(TArgs ...), TSize>::InvokerBound<TBound>::exec(TArgs ...) const [with TBound = Func1; long unsigned int TSize = 40ul; TRet = int; TArgs = {int}]’:
main.cpp:37:1:   required from here
StaticFunction.h:361:46: error: no match for call to ‘(const Func1) (int)’
     return func_(std::forward<TArgs>(args)...);
                                              ^
main.cpp:7:9: note: candidate: int Func1::operator()(int) <near match>
     int operator()(int val)
         ^~~~~~~~
main.cpp:7:9: note:   passing ‘const Func1*’ as ‘this’ argument discards qualifiers

(See it on Wandbox here.)

To get it to compile I need to change the functors' operator() to const, i.e.

struct Func1
{
    int operator()(int val) const
// .................
}

struct Func2
{
    int operator()(int val) const
// .................

Anyway... I tripped over this, when trying to get your embxx::util::StaticFunction to work with a benchmark. See here: https://github.com/user706/CxxFunctionBenchmark/tree/embxx . (It will only compile, if I put in the const here and here)

Regards, user706

arobenko commented 6 years ago

Thanks for the report. The fix should be available in "develop" branch, after some time and testing it will find its way to "master".

user706 commented 6 years ago

Great, it's working! Thanks. (See e.g. here)