I have a question about the Functor implementation of the library Loki.
I am doing some changes in order to make it work with variadic templates instead of having lines and lines of template specialization. The problem is that I am trying to use typedef for variadic template and I do not understand my error, that is why I could use some help from experts...
Here is the header file.
I tested it with a simple example:
static void foo()
{
std::cout << "foo !!!" << std::endl;
}
int
main( int argc, const char** argv )
{
Functor<void, void> static_func(foo);
static_func();
}
Which gives me this error
/home/test/src/EntryPoint.cpp:237:17: error: no match for call to ‘(Functor<void, void>) ()’
In file included from /home/test/src/EntryPoint.cpp:231:0:
/home/test/include/FunctorTest.h:217:7: note: candidate is:
/home/test/include/FunctorTest.h:292:16: note: Functor<R, TList>::ResultType Functor<R, TList>::operator()(Functor<R, TList>::MyList&&) const [with R = void; TList = {void}; Functor<R, TList>::ResultType = void; Functor<R, TList>::MyList = variadic_typedef<void>]
/home/test/include/FunctorTest.h:292:16: note: candidate expects 1 argument, 0 provided
/home/test/src/EntryPoint.cpp: At global scope:
/home/test/src/EntryPoint.cpp:234:1: warning: unused parameter ‘argc’ [-Wunused-parameter]
/home/test/src/EntryPoint.cpp:234:1: warning: unused parameter ‘argv’ [-Wunused-parameter]
In file included from /home/test/src/EntryPoint.cpp:231:0:
/home/test/include/FunctorTest.h: In instantiation of ‘FunctorHandler<ParentFunctor, Fun>::ResultType FunctorHandler<ParentFunctor, Fun>::operator()(FunctorHandler<ParentFunctor, Fun>::MyList&&) [with ParentFunctor = Functor<void, void>; Fun = void (*)(); FunctorHandler<ParentFunctor, Fun>::ResultType = void; FunctorHandler<ParentFunctor, Fun>::MyList = variadic_typedef<void>]’:
/home/test/src/EntryPoint.cpp:247:1: required from here
/home/test/include/FunctorTest.h:159:49: error: no matching function for call to ‘forward(FunctorHandler<Functor<void, void>, void (*)()>::MyList&)’
/home/test/include/FunctorTest.h:159:49: note: candidates are:
In file included from /usr/include/c++/4.7/bits/stl_pair.h:61:0,
from /usr/include/c++/4.7/utility:72,
from /home/jean/Lib/vitals/include/CLPair.h:28,
from /home/jean/Lib/vitals/include/CLMap.h:27,
from /home/jean/Lib/vitals/include/HTCmdLineParser.h:27,
from /home/test/include/EntryPoint.h:23,
from /home/test/src/EntryPoint.cpp:22:
/usr/include/c++/4.7/bits/move.h:77:5: note: template<class _Tp> constexpr _Tp&& std::forward(typename std::remove_reference<_From>::type&)
/usr/include/c++/4.7/bits/move.h:77:5: note: template argument deduction/substitution failed:
In file included from /home/test/src/EntryPoint.cpp:231:0:
/home/test/include/FunctorTest.h:159:49: note: cannot convert ‘parms’ (type ‘FunctorHandler<Functor<void, void>, void (*)()>::MyList {aka variadic_typedef<void>}’) to type ‘std::remove_reference<convert_in_tuple<variadic_typedef<void> > >::type& {aka convert_in_tuple<variadic_typedef<void> >&}’
Related
I have a class that will produce a set of values and pass them to callback, as defined by variadic template arguments:
template <typename... TResults>
class Statement
{
public:
using HandleValues = std::function<bool(TResults...)>;
void getValues(const HandleValues& hander);
}
My real code is templated SQL statement handler, so these are the types to be read from a database. For simplification, just imagine the values produced by these dummy methods:
// Some overloads exist that produce a value:
template <typename TRead>
struct Produce
{
static TRead value() {}
};
template <>
struct Produce<int>
{
static int value() { return 42; }
};
template <>
struct Produce<bool>
{
static bool value() { return true; }
};
In real code, these are conversion traits.
The problematic code is trying to put the produced values in the callback. I have tried this:
template <typename... TResults>
void Statement<TResults...>::getValues(const RowHandler& handler)
{
return handler((Produce<TResults>::value(), ...));
}
The error I get:
MSVC: error C2064: term does not evaluate to a function taking 1 arguments
GCC is more verbose:
./include/sqlite3++/Statement.h:84:3: required from 'void sqlitepp::Statement<TResults>::execute(const RowHandler&, TValRest ...) [with TValRest = {const char*}; TResults = {double, int}; sqlitepp::Statement<TResults>::RowHandler = std::function<bool(double, int)>]'
../build/examples/basic/BasicSQLite/BasicSQLite.cpp:52:5: required from here
../include/sqlite3++/Statement.h:94:19: error: no match for call to '(const RowHandler {aka const std::function<bool(double, int)>}) (int)'
94 | return handler((ReadTraits<TResults>::ReadFromStatement(reader), ...));
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\functional:59,
from ../include/sqlite3++/internal/RawStatement.h:14,
from ../include/sqlite3++/Statement.h:5,
from ../build/examples/basic/BasicSQLite/BasicSQLite.cpp:6:
c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\std_function.h:685:5: note: candidate: '_Res std::function<_Res(_ArgTypes ...)>::operator()(_ArgTypes ...) const [with _Res = bool; _ArgTypes = {double, int}]'
685 | function<_Res(_ArgTypes...)>::
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Clearly, the arguments are not actually expanding. I tried other variants, such as also putting ... after the template argument, but nothing has helped.
Use this return handler(Produce::value()...);
Can anyone help me with this errors. When i compile this simple program
#include<queue>
using namespace std;
template<typename Queue>
int qtest(Queue & queue,typename Queue::Type item)
{
return 0;
}
int main()
{
std::queue<int> q;
int t = qtest(q,3);
}
I get the errors like below
In function 'int main()':
error: no matching function for call to 'qtest(std::queue<int>&, int)'
note: candidate is:
note: template<class Queue> int qtest(Queue&, typename Queue::Type)
note: template argument deduction/substitution failed:
In substitution of 'template<class Queue> int qtest(Queue&, typename Queue::Type) [with
Queue = std::queue<int>]':
required from here
error: no type named 'Type' in 'class std::queue<int>'
warning: unused variable 't' [-Wunused-variable]
std::queue doesn't have a member type called Type. That's what the compiler is telling us. I'm guessing what you're looking for is std::queue<int>::value_type.
template<typename Queue>
int qtest(Queue & queue,typename Queue::value_type item)
{
return 0;
}
Reference: cppreference
I'm working on a C++ project that uses two different libraries: spdlog for logging, and mutils-serialization for serializing objects to bytes (for sending over the network). Both libraries use namespaces properly, but when I attempt to write a program that uses both of them at the same time, my compiler (g++ 6.2) gives me nonsensical errors that seem to indicate it is attempting to instantiate a function template from the spdlog library by using the definition of a function template from the mutils library.
Here's my simple test program:
#include <spdlog/spdlog.h>
#include <spdlog/fmt/ostr.h>
#include "TestSerializableObject.h"
int main(int argc, char** argv) {
auto global_logger = spdlog::rotating_logger_mt("global_logger", "log", 1024 * 1024 * 500, 3);
global_logger->set_pattern("[%H:%M:%S.%e] [%l] %v");
global_logger->set_level(spdlog::level::trace);
std::shared_ptr<spdlog::logger> logger(spdlog::get("global_logger"));
auto message = std::make_shared<messaging::TestSerializableObject>(
1, 2, "A message!");
logger->trace("Received a message: {}", *message);
}
TestSerializableObject is a simple class that implements mutils::ByteRepresentable (the interface that enables serialization and pulls in the mutils-serialization library), and provides an operator<< (which is required for spdlog to be able to log it). I can post the code for it if necessary.
When I compile this with g++ -std=c++14 -I"./src" -I"./libraries" -I"./libraries/mutils/" -L"./libraries/" -O0 -g3 -Wall "src/LibraryCollisionTest.cpp", I get this long, ugly error (don't worry, I'll help you parse it):
In file included from ./libraries/mutils/mutils.hpp:3:0,
from ./libraries/mutils-serialization/SerializationSupport.hpp:2,
from src/TestSerializableObject.h:10,
from src/LibraryCollisionTest.cpp:10:
./libraries/mutils/args-finder.hpp: In instantiation of ‘struct mutils::function_traits<messaging::TestSerializableObject>’:
./libraries/mutils/args-finder.hpp:75:41: required from ‘auto mutils::convert(F) [with F = messaging::TestSerializableObject; ignore = void]’
./libraries/spdlog/fmt/bundled/format.h:1276:46: required from ‘struct fmt::internal::ConvertToInt<messaging::TestSerializableObject>’
./libraries/spdlog/fmt/bundled/format.h:1485:5: required by substitution of ‘template<class T> fmt::internal::MakeValue<Formatter>::MakeValue(const T&, typename fmt::internal::EnableIf<fmt::internal::Not<fmt::internal::ConvertToInt<T>::value>::value, int>::type) [with T = messaging::TestSerializableObject]’
./libraries/spdlog/fmt/bundled/format.h:2465:12: required from ‘static fmt::internal::Value fmt::internal::ArgArray<N, true>::make(const T&) [with Formatter = fmt::BasicFormatter<char>; T = messaging::TestSerializableObject; unsigned int N = 1u]’
./libraries/spdlog/fmt/bundled/format.h:2898:5: required from ‘void fmt::BasicWriter<Char>::write(fmt::BasicCStringRef<CharType>, const Args& ...) [with Args = {messaging::TestSerializableObject}; Char = char]’
./libraries/spdlog/details/logger_impl.h:69:9: required from ‘void spdlog::logger::log(spdlog::level::level_enum, const char*, const Args& ...) [with Args = {messaging::TestSerializableObject}]’
./libraries/spdlog/details/logger_impl.h:127:5: required from ‘void spdlog::logger::trace(const char*, const Args& ...) [with Args = {messaging::TestSerializableObject}]’
src/LibraryCollisionTest.cpp:21:53: required from here
./libraries/mutils/args-finder.hpp:12:37: error: ‘operator()’ is not a member of ‘messaging::TestSerializableObject’
: public function_traits<decltype(&T::operator())>
^~
./libraries/mutils/args-finder.hpp: In instantiation of ‘auto mutils::convert(F) [with F = messaging::TestSerializableObject; ignore = void]’:
./libraries/spdlog/fmt/bundled/format.h:1276:46: required from ‘struct fmt::internal::ConvertToInt<messaging::TestSerializableObject>’
./libraries/spdlog/fmt/bundled/format.h:1485:5: required by substitution of ‘template<class T> fmt::internal::MakeValue<Formatter>::MakeValue(const T&, typename fmt::internal::EnableIf<fmt::internal::Not<fmt::internal::ConvertToInt<T>::value>::value, int>::type) [with T = messaging::TestSerializableObject]’
./libraries/spdlog/fmt/bundled/format.h:2465:12: required from ‘static fmt::internal::Value fmt::internal::ArgArray<N, true>::make(const T&) [with Formatter = fmt::BasicFormatter<char>; T = messaging::TestSerializableObject; unsigned int N = 1u]’
./libraries/spdlog/fmt/bundled/format.h:2898:5: required from ‘void fmt::BasicWriter<Char>::write(fmt::BasicCStringRef<CharType>, const Args& ...) [with Args = {messaging::TestSerializableObject}; Char = char]’
./libraries/spdlog/details/logger_impl.h:69:9: required from ‘void spdlog::logger::log(spdlog::level::level_enum, const char*, const Args& ...) [with Args = {messaging::TestSerializableObject}]’
./libraries/spdlog/details/logger_impl.h:127:5: required from ‘void spdlog::logger::trace(const char*, const Args& ...) [with Args = {messaging::TestSerializableObject}]’
src/LibraryCollisionTest.cpp:21:53: required from here
./libraries/mutils/args-finder.hpp:75:41: error: ‘as_function’ is not a member of ‘mutils::function_traits<messaging::TestSerializableObject>’
return function_traits<F>::as_function(f);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
In file included from ./libraries/spdlog/fmt/fmt.h:21:0,
from ./libraries/spdlog/common.h:41,
from ./libraries/spdlog/spdlog.h:12,
from src/LibraryCollisionTest.cpp:8:
./libraries/spdlog/fmt/bundled/format.h: In instantiation of ‘struct fmt::internal::ConvertToInt<messaging::TestSerializableObject>’:
./libraries/spdlog/fmt/bundled/format.h:1485:5: required by substitution of ‘template<class T> fmt::internal::MakeValue<Formatter>::MakeValue(const T&, typename fmt::internal::EnableIf<fmt::internal::Not<fmt::internal::ConvertToInt<T>::value>::value, int>::type) [with T = messaging::TestSerializableObject]’
./libraries/spdlog/fmt/bundled/format.h:2465:12: required from ‘static fmt::internal::Value fmt::internal::ArgArray<N, true>::make(const T&) [with Formatter = fmt::BasicFormatter<char>; T = messaging::TestSerializableObject; unsigned int N = 1u]’
./libraries/spdlog/fmt/bundled/format.h:2898:5: required from ‘void fmt::BasicWriter<Char>::write(fmt::BasicCStringRef<CharType>, const Args& ...) [with Args = {messaging::TestSerializableObject}; Char = char]’
./libraries/spdlog/details/logger_impl.h:69:9: required from ‘void spdlog::logger::log(spdlog::level::level_enum, const char*, const Args& ...) [with Args = {messaging::TestSerializableObject}]’
./libraries/spdlog/details/logger_impl.h:127:5: required from ‘void spdlog::logger::trace(const char*, const Args& ...) [with Args = {messaging::TestSerializableObject}]’
src/LibraryCollisionTest.cpp:21:53: required from here
./libraries/spdlog/fmt/bundled/format.h:1276:38: warning: invalid application of ‘sizeof’ to a void type [-Wpointer-arith]
enum { enable_conversion = sizeof(convert(get<T>())) == sizeof(Yes) };
The key line is here:
./libraries/mutils/args-finder.hpp: In instantiation of ‘auto mutils::convert(F)
[with F = messaging::TestSerializableObject; ignore = void]’:
./libraries/spdlog/fmt/bundled/format.h:1276:46: required from ‘struct
fmt::internal::ConvertToInt<messaging::TestSerializableObject>’
./libraries/spdlog/fmt/bundled/format.h:1485:5: required by substitution of
‘template<class T> fmt::internal::MakeValue<Formatter>::MakeValue(const
T&, typename fmt::internal::EnableIf<fmt::internal::Not<
fmt::internal::ConvertToInt<T>::value>::value, int>::type) [with T =
messaging::TestSerializableObject]’
Somehow, g++ has jumped from expanding a templated function inside the spdlog library, in namespace fmt::internal, to a function template in the mutils library, in namespace mutils, which is clearly not what the spdlog library intended to do! If I look at line 1276 of format.h, it's the one that calls the "convert" function inside this template struct:
template<typename T>
struct ConvertToInt
{
enum { enable_conversion = sizeof(convert(get<T>())) == sizeof(Yes) };
enum { value = ConvertToIntImpl2<T, enable_conversion>::value };
};
A few lines above, sure enough, is the function "convert":
template <typename T>
T &get();
Yes &convert(fmt::ULongLong);
No &convert(...);
These are all inside the namespace fmt::internal, and my IDE agrees that if I want the definition of the function "convert" on line 1276, I should jump to the function "convert" on line 1248. So why does g++ ignore this definition, and instead try to use the definition for mutils::convert(), which isn't even in the right namespace?
Note that clang also fails to compile this program, and makes the same mistake, so I don't think that this is a bug in g++.
This is definitively a bug in spdlog fmtlib, used internally by spdlog.
The problem is described summarily in this FAQ:
What is “Argument-Dependent Lookup” (aka ADL, or “Koenig Lookup”)?
Because messaging::TestSerializableObject inherits from a type in namespace mutils, when convert is called unqualified from inside namespace fmt::internal with a TestSerializableObject, both fmt::internal::convert and mutils::convert are considered in the overload set. Variadic functions always rank last during overload resolution, so the template argument F in the latter is a better match than the ... in the former and mutils::convert is chosen.
This is in no way specific to your code or to mutils – any type with a unary function or function template named convert in the same namespace or a parent namespace is susceptible to this problem.
The fix is to qualify the convert call and change the definition of fmt::internal::ConvertToInt<T>::enable_conversion from
enum { enable_conversion = sizeof(convert(get<T>())) == sizeof(Yes) };
to
enum { enable_conversion = sizeof(internal::convert(get<T>())) == sizeof(Yes) };
In my own code I make a habit of always qualifying all calls to functions inside any internal/detail namespace, even from code inside that same namespace, unless ADL usage is explicitly intended. (N.b. calls don't need to be fully qualified, merely qualified.) I learned this lesson from watching Boost have to deal with this problem the hard way as C++11 was emerging.
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 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.