Non standard code, or g++ error?

2019-07-27 11:36发布

问题:

Okay I've developed some code;

Linkage (it's rather long)

And when I compile it with Clang++ 3.2, it runs and results in;

stdout: 
print: C-3PO
print: R2D2

However if I try to compile it with G++ 4.7.2, I get these errors;

Compilation finished with errors:
source.cpp: In function 'int main()':
source.cpp:90:71: error: no matching function for call to 'makeRunnable(int (&)(char, int, const char*), char, int)'
source.cpp:90:71: note: candidate is:
source.cpp:74:27: note: template<class ... RUN_TIME, class T, class ... CONSTRUCTION_TIME> Runnable<T, RUN_TIME ...>* makeRunnable(T (*)(CONSTRUCTION_TIME ..., RUN_TIME ...), CONSTRUCTION_TIME ...)
source.cpp:74:27: note:   template argument deduction/substitution failed:
source.cpp:90:71: note:   mismatched types 'const char*' and 'char'
source.cpp:90:71: error: unable to deduce 'auto' from '<expression error>'
source.cpp:92:72: error: no matching function for call to 'makeRunnable(int (&)(char, int, const char*), char)'
source.cpp:92:72: note: candidate is:
source.cpp:74:27: note: template<class ... RUN_TIME, class T, class ... CONSTRUCTION_TIME> Runnable<T, RUN_TIME ...>* makeRunnable(T (*)(CONSTRUCTION_TIME ..., RUN_TIME ...), CONSTRUCTION_TIME ...)
source.cpp:74:27: note:   template argument deduction/substitution failed:
source.cpp:92:72: note:   mismatched types 'int' and 'char'
source.cpp:92:72: error: unable to deduce 'auto' from '<expression error>'

And almost the same with G++ 4.8.0 (although prettier formatted).

So the question is;

Is this code standard conformant? - If not why?

Edit Relevant code from link:

template<typename... RUN_TIME, typename T, typename... CONSTRUCTION_TIME> 
Runnable<T, RUN_TIME...>* makeRunnable(T (*FunctionType)(CONSTRUCTION_TIME..., RUN_TIME...), CONSTRUCTION_TIME... ct_args)  // Line 74
{
    return new FunctionDelegate<T,
                                std::tuple<CONSTRUCTION_TIME...>,
                                std::tuple<CONSTRUCTION_TIME..., RUN_TIME...>,
                                RUN_TIME...>(FunctionType, std::make_tuple(ct_args...));
}

int print_function(char arg1, int arg2, const char* arg3)
{
    std::cout << "print: " << arg1 << arg2 << arg3 << std::endl;
    return 2;
}

int main()
{   
    auto function1 = makeRunnable<const char*>(print_function, 'C', -3);  // Line 90
    int n = function1->invoke("PO");
    auto function2 = makeRunnable<int, const char*>(print_function, 'R');  // Line 92
    function2->invoke(n, "D2");
}

The point of this question, isn't really the implementation in question, more that Clang++ and G++ is not disagreeing on whether this is an error or not.

回答1:

Having played with the code a bit, it seems that g++ can't handle the deducing of variadic template concatenation (between the |!|).

Runnable<T, RUN_TIME...>* makeRunnable(T (*FunctionType)|!|(CONSTRUCTION_TIME..., RUN_TIME...)|!|, CONSTRUCTION_TIME... ct_args)

And the code is fixable, by adding another variadic template argument, such that g++ can deduce the template directly.