Mach7 icon indicating copy to clipboard operation
Mach7 copied to clipboard

Constructor pattern limited to at most 4 arguments?

Open forflo opened this issue 9 years ago • 2 comments

If I call the function Template C as follows

Match(foo){
    Case(C<SomeType>(arg0, arg1, arg2, arg3)){
        /* ... */
    }
    Otherwise(){/*...*/}
} EndMatch

everything works as expected. However, if I write,

Match(foo){
    Case(C<SomeType>(arg0, arg1, arg2, arg3, arg4)){
        /* ... */
    }
    Otherwise(){/*...*/}
} EndMatch

the compiler complains about no matching function for call to 'C'. I already looked at constructor.hpp and the implementations of the class templates constr{0,1,2,3,4}. I assume those to be the underlying base of C which almost certainly itself is a macro (is that right?), which would explain the limitation to 4 arguments of the call operator. But then I looked more closely and realized that only the number of template arguments changes. The call operator overloads (more or less) stay the same throughout the different versions of constr.

    template <typename U> const T* operator()(const U* u) const noexcept { return dynamic_cast<const T*>(u); }
    template <typename U>       T* operator()(      U* u) const noexcept { return dynamic_cast<      T*>(u); }
    template <typename U> const T* operator()(const U& u) const noexcept { return operator()(&u); }
    template <typename U>       T* operator()(      U& u) const noexcept { return operator()(&u); }
                          const T* operator()(const T* t) const noexcept { return t; }
                                T* operator()(      T* t) const noexcept { return t; }
                          const T* operator()(const T& t) const noexcept { return &t; }
                                T* operator()(      T& t) const noexcept { return &t; }

Why is that? I'm probably missing something obvious here... Could you elaborate the expansion of C a little further. Somehow I can't find the macro definition. I also tried to compile with gcc -E in order to view the expansions, without much success (however, the output did hurt my eyes).

Best regards Florian

forflo avatar Jul 03 '16 13:07 forflo

Yes, that code was written prior to variadic arguments making into the language, so i only bothered to put specializations of up to as many arguments as the examples i've been working with at the time. I'd add a variadic implementation to handle the general case for the compilers that support it.

Note that with Members macro the issue can't be solved with variadic templates, so you'd have to tell me how many arguments would be sufficient for your use cases.

solodon4 avatar Jul 03 '16 22:07 solodon4

have to tell me how many arguments would be sufficient for your use cases.

I already resolved it using normal member access, but thank you very much for the support!

forflo avatar Jul 04 '16 17:07 forflo