Non standard code, or g++ error? - c++

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.

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.

Related

"no matching function for call to" when having a function pointer with template arguments as a template argument

I'm writing a template wrapper function that can be applied to a functions with different number/types of arguments.
I have some code that works but I'm trying to change more arguments into template parameters.
The working code:
#include <iostream>
int func0(bool b) { return b ? 1 : 2; }
//There is a few more funcX...
template<typename ...ARGS>
int wrapper(int (*func)(ARGS...), ARGS... args) { return (*func)(args...) * 10; }
int wrappedFunc0(bool b) { return wrapper<bool>(func0, b); }
int main()
{
std::cout << wrappedFunc0(true) << std::endl;
return 0;
}
Now I want int (*func)(ARGS...) to also be a template parameter. (It's for performance reasons. I want the pointer to be backed into the wrapper, because the way I'm using it prevents the compiler from optimizing it out.)
Here is what I came up with (The only difference is I've changed the one argument into a template parameter.):
#include <iostream>
int func0(bool b) { return b ? 1 : 2; }
//There is a few more funcX...
template<typename ...ARGS, int (*FUNC)(ARGS...)>
int wrapper(ARGS... args) { return (*FUNC)(args...) * 10; }
int wrappedFunc0(bool b) { return wrapper<bool, func0>(b); }
int main()
{
std::cout << wrappedFunc0(true) << std::endl;
return 0;
}
This doesn't compile. It shows:
<source>: In function 'int wrappedFunc0(bool)':
<source>:9:55: error: no matching function for call to 'wrapper<bool, func0>(bool&)'
9 | int wrappedFunc0(bool b) { return wrapper<bool, func0>(b); }
| ~~~~~~~~~~~~~~~~~~~~^~~
<source>:7:5: note: candidate: 'template<class ... ARGS, int (* FUNC)(ARGS ...)> int wrapper(ARGS ...)'
7 | int wrapper(ARGS... args) { return (*FUNC)(args...) * 10; }
| ^~~~~~~
<source>:7:5: note: template argument deduction/substitution failed:
<source>:9:55: error: type/value mismatch at argument 1 in template parameter list for 'template<class ... ARGS, int (* FUNC)(ARGS ...)> int wrapper(ARGS ...)'
9 | int wrappedFunc0(bool b) { return wrapper<bool, func0>(b); }
| ~~~~~~~~~~~~~~~~~~~~^~~
<source>:9:55: note: expected a type, got 'func0'
ASM generation compiler returned: 1
<source>: In function 'int wrappedFunc0(bool)':
<source>:9:55: error: no matching function for call to 'wrapper<bool, func0>(bool&)'
9 | int wrappedFunc0(bool b) { return wrapper<bool, func0>(b); }
| ~~~~~~~~~~~~~~~~~~~~^~~
<source>:7:5: note: candidate: 'template<class ... ARGS, int (* FUNC)(ARGS ...)> int wrapper(ARGS ...)'
7 | int wrapper(ARGS... args) { return (*FUNC)(args...) * 10; }
| ^~~~~~~
<source>:7:5: note: template argument deduction/substitution failed:
<source>:9:55: error: type/value mismatch at argument 1 in template parameter list for 'template<class ... ARGS, int (* FUNC)(ARGS ...)> int wrapper(ARGS ...)'
9 | int wrappedFunc0(bool b) { return wrapper<bool, func0>(b); }
| ~~~~~~~~~~~~~~~~~~~~^~~
<source>:9:55: note: expected a type, got 'func0'
Execution build compiler returned: 1
(link to the compiler explorer)
It looks like a problem with the compiler to me, but GCC and Clang agree on it so maybe it isn't.
Anyway, how can I make this template compile correctly with templated pointer to a function?
EDIT:
Addressing the duplicate flag Compilation issue with instantiating function template
I think the core of the problem in that question is the same as in mine, however, it lacks a solution that allows passing the pointer to function (not only its type) as a template parameter.
This doesn't work because a pack parameter (the one including ...) consumes all remaining arguments. All arguments following it can't be specified explicitly and must be deduced.
Normally you write such wrappers like this:
template <typename F, typename ...P>
int wrapper(F &&func, P &&... params)
{
return std::forward<F>(func)(std::forward<P>(params)...) * 10;
}
(And if the function is called more than once inside of the wrapper, all calls except the last can't use std::forward.)
This will pass the function by reference, which should be exactly the same as using a function pointer, but I have no reasons to believe that it would stop the compiler from optimizing it.
You can force the function to be encoded in the template argument by passing std::integral_constant<decltype(&func0), func0>{} instead of func0, but again, I don't think it's going to change anything.
The 2nd snippet is not valid because:
a type parameter pack cannot be expanded in its own parameter clause.
As from [temp.param]/17:
If a template-parameter is a type-parameter with an ellipsis prior to its optional identifier or is a parameter-declaration that declares a pack ([dcl.fct]), then the template-parameter is a template parameter pack. A template parameter pack that is a parameter-declaration whose type contains one or more unexpanded packs is a pack expansion. ... A template parameter pack that is a pack expansion shall not expand a template parameter pack declared in the same template-parameter-list.
So consider the following invalid example:
template<typename... Ts, Ts... vals> struct mytuple {}; //invalid
The above example is invalid because the template type parameter pack Ts cannot be expanded in its own parameter list.
For the same reason, your code example is invalid. For example, a simplified version of your 2nd snippet doesn't compile in msvc.

Why can't this parameter pack accept function pointers?

I'm trying to create a parameter pack full of function pointers, but GCC (with c++17 standard) generates a deduction failed error. Why is that?
As written here:
For pointers to functions, the valid arguments are pointers to functions with linkage (or constant expressions that evaluate to null pointer values).
In my example, that's the case (isn't it?).
Is this rule invalidated for parameter packs? Did I miss something in the standard? If that's the case, how can I fix my code, without passing the function pointers as function arguments (ie without declaring T run2(T input, Funcs... funcs).
// In f.hpp
template<typename T>
T run2(T input)
{
return input;
}
template<typename T, T(*f)(T), class ... Funcs>
T run2(T input)
{
return run2<T, Funcs...>(f(input));
}
// In m.cpp
unsigned add2(unsigned v)
{
return v+2;
}
int main()
{
unsigned a=1;
a = run2<unsigned, add2>(a); // works
a = run2<unsigned, add2, add2>(a); // doesn't work
std::cout << a << std::endl;
return 0;
}
This the error I get with run2<unsigned, add2, add2> (GCC doesn't tell me why the last attempt actually failed):
m.cpp: In function ‘int main()’:
m.cpp:37:37: error: no matching function for call to ‘run2(unsigned int&)’
a = run2<unsigned, add2, add2>(a);
^
In file included from m.cpp:2:0:
./f.hpp:85:3: note: candidate: template<class T> T run2(T)
T run2(T input)
^
./f.hpp:85:3: note: template argument deduction/substitution failed:
m.cpp:37:37: error: wrong number of template arguments (3, should be 1)
a = run2<unsigned, add2, add2>(a);
^
In file included from m.cpp:2:0:
./f.hpp:109:3: note: candidate: template<class T, T (* f)(T), class ... Funcs> T run2(T)
T run2(T input)
^
./f.hpp:109:3: note: template argument deduction/substitution failed:
You declared a type parameter pack, class... Funcs. You can't pass function pointers as arguments for type parameters, because they are values, not types. Instead, you need to declare the run2 template so that it has a function pointer template parameter pack. The syntax to do so is as follows:
template<typename T, T(*f)(T), T(*...fs)(T)>
T run2(T input)
{
return run2<T, fs...>(f(input));
}
(The rule is that the ... is part of the declarator-id and goes right before the identifier, namely fs.)
The pack fs can accept one or more function pointers of type T (*)(T).

GCC cannot deduce auto return type from a template function?

I've a simple template function do_something which returns an integer: 123.
template<typename T>
auto do_something(T input) {
std::this_thread::sleep_for(std::chrono::seconds(1));
return 123;
}
int main(int argc, char *argv[]) {
std::function<int(void)> function = std::bind(do_something<int>, 12);
function();
return 0;
}
With GCC 6.1.1, I get this error:
test.cpp: In function ‘int main(int, char**)’:
test.cpp:16:70: error: no matching function for call to ‘bind(<unresolved overloaded function type>, int)’
std::function<int(void)> function = std::bind(do_something<int>, 12);
^
In file included from /usr/include/c++/6.1.1/thread:39:0,
from test.cpp:4:
/usr/include/c++/6.1.1/functional:1331:5: note: candidate: template<class _Func, class ... _BoundArgs> typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...)
bind(_Func&& __f, _BoundArgs&&... __args)
^~~~
/usr/include/c++/6.1.1/functional:1331:5: note: template argument deduction/substitution failed:
test.cpp:16:70: note: couldn't deduce template parameter ‘_Func’
std::function<int(void)> function = std::bind(do_something<int>, 12);
^
In file included from /usr/include/c++/6.1.1/thread:39:0,
from test.cpp:4:
/usr/include/c++/6.1.1/functional:1359:5: note: candidate: template<class _Result, class _Func, class ... _BoundArgs> typename std::_Bindres_helper<_Result, _Func, _BoundArgs>::type std::bind(_Func&&, _BoundArgs&& ...)
bind(_Func&& __f, _BoundArgs&&... __args)
^~~~
/usr/include/c++/6.1.1/functional:1359:5: note: template argument deduction/substitution failed:
test.cpp:16:70: note: couldn't deduce template parameter ‘_Result’
std::function<int(void)> function = std::bind(do_something<int>, 12);
As you can see, the compiler cannot deduce the result type of the function.
Note that: clang++ 3.8.0 can compile that without any errors.
So my question: is there a way to specify the expected return value from a template function like in this case?
It looks like the compiler isn't sure about the type of the do_something<int> - and I'm not sure if this is a compiler issue, or a language issue - but you can force the compiler to get its types sorted out by using do_something<int> in a relatively trivial way before hand. For example the following compiles OK with both gcc and clang trunk versions (according to godbolt).
#include <functional>
template<typename T>
auto do_something(T input) {
return 123;
}
// Make the compiler workout the type of do_something<int> so we can use it later.
auto f = do_something<int>;
int main(int argc, char *argv[]) {
std::function<int(void)> function = std::bind(do_something<int>, 12);
function();
return 0;
}

How to pass function argument to boost::async()

How do I get boost::async to pass a function argument to the function it executes. My code is
#include <iostream>
// This is for the boos version
#include <boost/thread/future.hpp>
using boost::async;
// This is for the standard library version
//~ #include <future>
//~ using std::async;
int f(int x) {
std::cout << " in f " << x << std::endl;
return x+1;
}
int main() {
auto f_res = async(f,3);
std::cout << "f " << f_res.get() << std::endl;
return 0;
}
I compile with
g++ -std=c++14 -lboost_thread -lboost_system -lpthread test_async_boost.cc
, g++ version 5.3.0 and get many errors, complaining about the number of arguments async is willing to take:
test_async_boost_2.cc: In function 'int main()':
test_async_boost_2.cc:16:26: error: no matching function for call to 'async(int (&)(int), int)'
auto f_res = async(f,3);
^
In file included from test_async_boost_2.cc:3:0:
/usr/include/boost/thread/future.hpp:4035:3: note: candidate: template<class F> boost::unique_future<typename boost::result_of<F()>::type> boost::async(F&&)
async(BOOST_THREAD_FWD_REF(F) f) {
^
/usr/include/boost/thread/future.hpp:4035:3: note: template argument deduction/substitution failed:
test_async_boost_2.cc:16:26: note: candidate expects 1 argument, 2 provided
auto f_res = async(f,3);
^
In file included from test_async_boost_2.cc:3:0:
/usr/include/boost/thread/future.hpp:4018:3: note: candidate: template<class R> boost::unique_future<T> boost::async(R (*)())
async(R(*f)()) {
^
/usr/include/boost/thread/future.hpp:4018:3: note: template argument deduction/substitution failed:
test_async_boost_2.cc:16:26: note: candidate expects 0 arguments, 1 provided
auto f_res = async(f,3);
^
In file included from test_async_boost_2.cc:3:0:
/usr/include/boost/thread/future.hpp:3695:3: note: candidate: template<class F> boost::unique_future<typename boost::result_of<typename boost::decay<T>::type()>::type> boost::async(boost::launch, F&&)
async(launch policy, BOOST_THREAD_FWD_REF(F) f) {
^
/usr/include/boost/thread/future.hpp:3695:3: note: template argument deduction/substitution failed:
test_async_boost_2.cc:16:26: note: cannot convert 'f' (type 'int(int)') to type 'boost::launch'
auto f_res = async(f,3);
^
In file included from test_async_boost_2.cc:3:0:
/usr/include/boost/thread/future.hpp:3634:3: note: candidate: template<class R> boost::unique_future<T> boost::async(boost::launch, R (*)())
async(launch policy, R(*f)()) {
^
/usr/include/boost/thread/future.hpp:3634:3: note: template argument deduction/substitution failed:
test_async_boost_2.cc:16:26: note: cannot convert 'f' (type 'int(int)') to type 'boost::launch'
auto f_res = async(f,3);
^
If I switch the commenting in the #include directuves and the using lines, and compile with
g++ -std=c++14 test_async_boost_2.cc -lpthread
I get the desired output:
in f 3
f 4
How do I get boost::async to work with function arguments?
And: Where do I find the reference documentation of boos::async?
In order to pass a function with its arguments to the method accepting a function, you have to use std::bind or boost::bind.

deduce of argument of type class method (overloads by const qualifier) fails with trailing return type in gcc, but not in clang

Nothing clearer than an old good MCVE:
struct X {
auto get(int) const -> int { return {}; }
auto get(int) -> int { return {}; }
};
template <class R> auto f(auto (X::*)(int) const -> R) {}
// ^~~~ ~~~~
// trailing return type
int main() {
f(&X::get);
}
This fails in g++ (4.9.2 & 5.1.0). However if the old return type is used:
template <class R> auto f(R (X::*)(int) const) {}
// ^
// old return type
it works.
On clang (3.5.0) both variants work.
I know that trailing return type changes when the return type is inferred and the scope of it, so I wouldn't be quick to cast it as a gcc bug. So what does the standard says? Which compiler is right?
The most significant message in the error I think is
couldn't deduce template parameter ‘R’`
g++ full message:
main2.cpp: In function ‘int main()’:
main2.cpp:21:12: error: no matching function for call to ‘f(<unresolved overloaded function type>)’
f(&X::get);
^
main2.cpp:18:25: note: candidate: template<class R, class auto:1> auto f(auto:1 (X::*)(int) const)
template <class R> auto f(auto (X::*)(int) const -> R) {}
^
main2.cpp:18:25: note: template argument deduction/substitution failed:
main2.cpp:21:12: note: types ‘auto:1 (X::)(int) const’ and ‘int (X::)(int)’ have incompatible cv-qualifiers
f(&X::get);
^
main2.cpp:21:12: note: couldn't deduce template parameter ‘R’
<builtin>: recipe for target 'main2' failed
make: *** [main2] Error 1
As pointed in the question this is a gcc bug which was beed fixed in version 6
gcc.gnu.org/bugzilla/show_bug.cgi?id=69139