c++ functions as template arguments - c++

I'm experiencing some problems which can be resumed by the following piece of code:
template <typename Key, typename Data, typename fct>
size_t wrapper(const std::pair<Key, Data> & p)
{
return fct(p.first);
}
int main(int argc, char *argv[])
{
size_t val =
wrapper<int, int, dft_hash_fct<int>>(std::pair<int,int>(5,9));
return 0;
}
I'm using clang compiler version 3.4 and this code does not compile with the following error
test-tmp.C:17:5: error: no matching function for call to 'wrapper'
wrapper<int, int, dft_hash_fct<int>>(std::pair<int,int>(5,9));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test-tmp.C:9:8: note: candidate template ignored: invalid explicitly-specified argument
for template parameter 'fct'
The idea is to wrap a hash function (the template parameter fct) on std::pair for only taking the first field.
dft_hash_fct is another template defines as follows:
template <typename Key>
size_t dft_hash_fct(const Key & key)
{
return SuperFastHash(key);
}
This generic function works; it has been used in other contexts.
The purpose of all this is to reuse a hash based set (not map) as a map of keys to items of any type. The hash based ser receives a hash function in construction time.
Thanks for your comments (David, Andrey and Kazark)
Edited:
Well, I see, typename fct is a type, so I cannot handle as a pointer function; sorry for the trivia. Unfortunately, I believe that the approach of passing the function as parameter in the wrapper does not work, because the hash set expects a function pointer with the following signature:
size_t (*the_function)(const Key & key);
So, realizing this, thanks to your observations, I changed the code in question to:
template <typename Key, typename Data, size_t (*fct)(const Key & k)>
size_t wrapper(const std::pair<Key, Data> & p)
{
return (*fct)(p.first);
}
int main(int argc, char *argv[])
{
size_t val =
wrapper<int, int, dft_hash_fct<int>>(std::pair<int,int>(5,9));
return 0;
}
that compiles, links and runs. In addition, I put this line:
size_t (*fct)(const std::pair<int, int>&) =
wrapper<int, int, dft_hash_fct<int>>;
cout << (*fct)(std::pair<int, int>(4,6)) << endl;
And that compiles, links ans runs too. So, I can say that the compiler (and of course according to the language) can instantiate the function and handle a function pointer to it.
So, after that I tried to modify my original code, which is a derived class of HAshSet intended for managing pairs hashed by first field.
I declare some as:
template <typename Key, typename Data>
class HashMap : public HashSet<std::pair<Key, Data>>
{
...
HashMap(size_t (*function)(const Key & key))
: HashSet<Key, Data>(wrapper<Key, Data, function>)
{
}
..
};
But the compilation (with std=c++11) fails with the error
./tpl_dynSetHash.H:353:7: error: no matching constructor for initialization of
'HashSet<std::pair<unsigned long, long>>'
: HashSet<std::pair<Key,Data>(
^
testDynSetHash.C:178:8: note: in instantiation of member function
'HashMap<unsigned long, long>::HashMap' requested here
HMap table;
However, if I substitute the call to base constructor by
: HashSet<Key, Data>(wrapper<Key, Data, dft_hash_fct<Key>)
That compiles fine. Thus, I believe that the problem is with the parameter type declaration (but I do not know what is).

The standard idiom to pass functions is to pass them as function objects, e.g.
template <typename Key, typename Data, typename Fct>
size_t wrapper(const std::pair<Key, Data> & p, Fct fct)
{
return fct(p.first);
}
Then call the wrapper using:
int main(int argc, char *argv[])
{
// no explicit template arguments required
size_t val =
wrapper(std::pair<int,int>(5,9), &dft_hash_fct<int>);
return 0;
}
In your code, on the other hand:
template <typename Key, typename Data, typename fct>
size_t wrapper(const std::pair<Key, Data> & p)
{
return fct(p.first);
}
typename fct introduces an alias for a type. Inside this function, fct names a type; therefore fct(p.first) creates an object of type fct, and this object needs to be converted to a size_t in order to return it from wrapper. You can use this as well, but the type you had to use would have to look like this:
struct dft_hash_fct_t
{
size_t result;
dft_hash_fct_t(int p) : result(SuperFashHash(p)) {}
operator size_t() const { return result; }
};
Which is probably not what you intended.

The template declaration in
template <typename Key, typename Data, typename fct>
size_t wrapper(const std::pair<Key, Data> & p)
{
return fct(p.first);
}
declares template parameterfct as a type, but you are trying to pass a function pointer to it. You can make fct function pointer template parameter like this:
template <typename Key, typename Data, size_t(*fct)(const Key&)>
size_t wrapper(const std::pair<Key, Data> & p)
{
return fct(p.first);
}
However, the more idiomatic way is (as DyP says) to pass a function object so that the function works with function pointers as well as objects overloading operator():
template <typename Key, typename Data, typename Fct>
size_t wrapper(const std::pair<Key, Data> & p, Fct fct)
{
return fct(p.first);
}
Then when calling it you pass the function as a parameter
wrapper(std::pair<int,int>(5,9), dft_hash_fct<int>);

The code you wrote makes no sense within the context of your intent. Your fct template parameter is a type. That means that
return fct(p.first);
is a function-style cast, not an application of () operator (i.e. it is not a function call). In your code you are attempting to cast p.first to type fct and then attempting to return the result of that cast as size_t. Was that your intent? I doubt that it was. On top of that you are trying to pass a function pointer value dft_hash_fct<int> as a template argument for fct, i.e. you are passing a value where a type is expected. How did you expect it to work?
The description you provided seems to imply that you actually wanted to call a functor with type fct from inside wrapper instead of performing a cast. In order to do that you have to obtain the functor itself somehow. Remember again that fct is not a functor, its is just the type of the functor.
The typical approach would be to pass the functor from the outside, as function parameter
template <typename Key, typename Data, typename fct>
size_t wrapper(const std::pair<Key, Data> & p, fct f)
{
return f(p.first);
}
Now you can use your wrapper template with class-based functors, as well as with ordinary functions
size_t val = wrapper(std::pair<int,int>(5,9), dft_hash_fct<int>);
Note that dft_hash_fct<int> has to be supplied as function argument, not as template argument.
There's no need to explicitly specify template arguments, since they will be deduced by the compiler.

Related

C++ template function pointer type deduction

I have the following code
using my_map_t = std::map<int, Object<double>>;
using my_map_iterator_t = my_map_t::iterator;
template <typename FWD, typename Func>
void inner(my_map_t& map, int key, FWD&& obj, Func emplacer) {
emplacer(map, key, std::forward<FWD>(obj));
}
my_map_t map;
template <typename FWD>
void outer(my_map_t& map, int key, FWD&& obj)
{
auto lambda = [](my_map_t& m, int k, FWD&& o) {
m.emplace(k, std::forward<FWD>(o));
};
inner(map, key, std::forward<FWD>(obj), lambda);
}
which compiles painlessly. So this means that he deduce automatically the template argument of inner function.
However, if I introduce a function pointer I need to specify the function pointer template argument, otherwise the compiler complaints
using my_map_t = std::map<int, Object<double>>;
using my_map_iterator_t = my_map_t::iterator;
template <typename FWD, typename Func>
void inner(my_map_t& map, int key, FWD&& obj, Func emplacer) {
emplacer(map, key, std::forward<FWD>(obj));
}
template <typename FWD, typename Func>
void(*fp)(my_map_t&, int, FWD&&, Func) = &inner;
my_map_t map;
template <typename FWD>
void outer(my_map_t& map, int key, FWD&& obj)
{
auto lambda = [](my_map_t& m, int k, FWD&& o) {
m.emplace(k, std::forward<FWD>(o));
};
(*fp<FWD, decltype(lambda)>)(map, key, std::forward<FWD>(obj), lambda);
}
Why in the case of function pointer the argument deduction is not working any more?
Did I make some mistake? Is there a way to achieve a better syntax?
NOTE ADDED
I need to use function pointer because I have some function with the same signature of inner. Let us call them inner1, inner2 and inner3. They are called by some outer function
void outer(...) {
if(...) {
inner1(...)
} else if (...) {
inner2(...)
} else {
inner3(...)
}
some_long_task(...)
}
Now in my case the outer function is called cyclically. The checks in the ifs can be time consuming and they are independent of the argument of outer function. I was thinking while the some_long_task is being executed, to set up a function pointer to the right function inner1, inner2 or inner3 exploiting some cpu parallelism, so that, the the new cycle begin, I do not have to waste time doing the if check.
template <typename FWD, typename Func>
void(*fp)(my_map_t&, int, FWD&&, Func) = &inner;
is not a normal function pointer. It is what's called a variable template. When you have a variable template, in order to refer to a specific instantiation, you must specify the template parameters. To demonstratre that, consider
template <typename T>
constexpr T pi = T(3.1415926535897932385L);
You can't just do cout << pi because we don't know which pi to use. The same occurs with
(*fp<FWD, decltype(lambda)>)(map, key, std::forward<FWD>(obj), lambda);
Here fp needs the <FWD, decltype(lambda)> so the compiler can know which specific fp instance to refer to. It has to do this even before it evaluates the function call because it needs to check the parameters against the function.
What we would need to not have to specify the parameter is a future like CTAD but would work for function pointer variable temapltes.

Issue with invalid use of incomplete type when using std::tuple_element

The following code implements a hash function for a std::tuple which is then used in a different segment of my code-base in a std::unordered_map of std::tuples.
// compute hash function recursively through each std::tuple element
template<class Tuple, std::size_t N>
struct tuple_hash_compute {
static std::size_t hash_compute(const Tuple& t) {
using type = typename std::tuple_element<N-1, decltype(t)>::type; // OFFENDING LINE
return tuple_hash_compute<Tuple, N-1>::hash_compute(t)
+ std::hash<type>()(std::get<N-1>(t));
}
};
// base helper
template<class Tuple>
struct tuple_hash_compute<Tuple, 1> {
static std::size_t hash_compute(const Tuple& t) {
using type = typename std::tuple_element<0, decltype(t)>::type; // OFFENDING LINE
return 51U + std::hash<type>()(std::get<0>(t))*51U;
}
};
// tuple_hash function object
struct tuple_hash {
template<class... Args>
std::size_t operator()(const std::tuple<Args...>& t) const {
return tuple_hash_compute<decltype(t), sizeof...(Args)>::hash_compute(t);
}
// will use std::unordered_map of std::pair too, so overload reqd
template<class Ty1, class Ty2>
std::size_t operator()(const std::pair<Ty1, Ty2>& p) const {
return tuple_hash_compute<decltype(t), 2>::hash_compute(p);
}
};
Then, just as an example, I would use this hash function-object like so,
std::unordered_map<std::tuple<int,int,int>, std::size_t, tuple_hash> agg_map;
agg_map.insert(std::make_pair(std::make_tuple(1,2,3), 0U));
agg_map.insert(std::make_pair(std::make_tuple(4,5,6), 1U));
However, in both GCC 6.1.0 and MSVC2015, I receive the following errors (both the same for each offending line above):
error: invalid use of incomplete type 'class std::tuple_element<2ul, const std::tuple<int,int,int>&>'
I'm not entirely sure what's causing this error (though it may be due to "abstraction" of passing the std::tuple via the template parameter Tuple) or how it can be solved so any help is appreciated.
For a parameter declared as below:
const Tuple& t
decltype(t) yields:
const Tuple&
Similarly, for a parameter declared as:
const std::pair<Ty1, Ty2>& t
decltype(t) yields:
const std::pair<Ty1, Ty2>&
In both cases, the produced type is a reference to a tuple-like type. However, std::tuple_element is not specialized for references, which means, the compiler falls back to the primary, undefined class template:
template <size_t I, typename T> class tuple_element;
What you want, is a plain Tuple in the former case, and std::pair<Ty1, Ty2> in the latter case.
If you have this problem with a type Tuple that may or may not be a reference, you can use std::remove_reference like so:
typename std::tuple_element<num, typename std::remove_reference<Tuple>::type>::type
or, for C++17,
std::tuple_element_t<num, std::remove_reference_t<Tuple>>
PS: I don't think it works with std::reference_wrapper though...

default value for template function, functional parameter

template<typename Iterator, typename typename Comparator = std::less<typename std::iterator_traits<Iterator>::value_type>>
static void sort(Iterator begin, Iterator end, Comparator cmp = Comparator())
{
...
}
I have the following template function:
template<typename func>
static void sort_test(func sort)
{
...
sort(somevector.begin(), somevector.end());
...
}
int main()
{
sort_test(&sort<vector<int, allocator<int>>::iterator>);
return 0;
}
error C2198: 'void (__cdecl *)(iterator,iterator,std::less)' : too few arguments for call
If I try to bypass the default argument by providing it:
template<typename func>
static void sort_test(func sort)
{
...
sort(somevector.begin(), somevector.end(), std::less<int>);
...
}
int main()
{
sort_test(&sort<vector<int, allocator<int>>::iterator>,
std::less<int>);
return 0;
}
error C2275: 'std::less' : illegal use of this type as an expression
Default arguments are not part of a function's signature, so when you go through a pointer to function indirection, as you're doing in the first example, that information is lost. If you call your sort function directly within main with 2 iterator arguments, the code will compile.
In the second example you're getting a compilation error because you're trying to pass a type, instead of an instance, to sort
sort(somevector.begin(), somevector.end(), std::less<int>());
You also have an extra typename in the template parameter list for sort
template<typename Iterator, typename typename Comparator = std::less<typename std::iterator_traits<Iterator>::value_type>>
// ^^^^^^^^

Standard C++ function object template for the subscript operator

Say I currently have a template function like this:
template <class T, class K>
void* get_subobject(K key)
{
T& obj = function_returning_T_ref<T>();
// do various other things...
return &obj[key];
}
And I would like to make the subscript operation configurable so that the user could apply their own code to map obj and key to the return value. Something like this:
template <class T, class K, class Op = subscript<T, K>>
void* get_subobject(K key)
{
T& obj = function_returning_T_ref<T>();
// do various other things...
return &Op{}(obj, key);
}
My question is, for the default template parameter subscript<T,K> above is there a standard template (along the lines of std::less<T>) that I can use here so that Op defaults to calling operator[]? I can't see anything appropriate in <functional>.
If there is no standard template for this, am I best to create my own or is there some way I can use std::bind() or similar to the same effect without additional overhead?
I don't know of any built-in template, but it's not too hard to create your own (that, once inlined, will have no overhead):
template<typename T, typename K>
struct subscript
{
inline auto operator()(T const& obj, K const& key) const -> decltype(obj[key])
{
return obj[key];
}
inline auto operator()(T& obj, K const& key) const -> decltype(obj[key])
{
return obj[key];
}
};
You could even have one that worked on implicit types (I like this one best):
struct subscript
{
template<typename T, typename K>
inline auto operator()(T&& obj, K&& key) const
-> decltype(std::forward<T>(obj)[std::forward<K>(key)])
{
return std::forward<T>(obj)[std::forward<K>(key)];
}
};
The user, of course, can pass in any conforming type of their own, including a std::function object or plain function pointers.

Getting rid of references in an boost::fusion sequence

I'm trying to use Boost::Fusion to transform a list of function's parameter types into a fusion::list. Ultimately, I am trying to turn a list of variables into parameters that I can call a function with (http://stackoverflow.com/questions/11164914/generating-wrappings-for-c-functions).
I've gotten this to work for non-referenced variables. However, it fails to compile for non-referenced variables when I try to turn the function's parameter list (specifically on the fusion::to_list it complains it can't deref the iterator).
I've simplified the code down by a bit below:
struct identity {
template<typename Sig> struct result;
template <typename T>
struct result<convert(T)> { typedef T type; };
template <typename T>
typename T operator ()(T) const {
return T();
}
};
int main(int argc, char **argv) {
typedef BOOST_TYPEOF(foo) params_type;
auto seq = function_types::parameter_types<params_type>();
auto transformed = fusion::transform(seq, identity());
auto passedParams = fusion::as_list(transformed);
}
If foo is defined as:
int foo(int a) { return 5*a; }
it works fine, but it breaks on:
int foo(int &a) { return 5*a; }
For the purposes of my code, I don't actually need the references kept in the sequence, which I am assuming is the issue (also, searches I've done tend to point that as being the culprit). However, I'm not completely sure of how to strip the transformed function of these references before as_list is called.
I tried something along the lines of:
template <typename T>
struct result<convert(T)>: remove_reference<T> {};
template <typename T>
typename remove_reference<T>::type operator ()(remove_reference<T>::type) const { return typename remove_reference<T>::type(); }
but got the same compile errors.
Any ideas of how to fix this?
update
Here is the truncated compiler error I get (with clang++ --std=c++0x) for both cases given above:
/usr/local/include/boost/fusion/adapted/mpl/mpl_iterator.hpp:43:24: error:
reference to type 'int' requires an initializer
return type();
^
/usr/local/include/boost/fusion/iterator/deref.hpp:61:28: note: in instantiation
of member function
'boost::fusion::mpl_iterator<boost::mpl::v_iter<boost::function_types::parameter_types<void
(int &), boost::add_reference<mpl_::arg<-1> > >, 0>
>::deref<boost::fusion::mpl_iterator<boost::mpl::v_iter<boost::function_types::parameter_types<void
(int &), boost::add_reference<mpl_::arg<-1> > >, 0> > >::call' requested
here
return deref_meta::call(i);
...
test4.cpp:65:22: note: in instantiation of function template specialization
'boost::fusion::as_list<boost::fusion::transform_view<const
boost::function_types::parameter_types<void (int &),
boost::add_reference<mpl_::arg<-1> > >, convert, boost::fusion::void_> >'
requested here
auto passedParams = fusion::as_list(transformed);
If your compiler is C++11 compatible, you might want to look into the `std::remove_reference function. Or at least try to find an implementation of it and use as reference for making your own.