I have this small piece of code:
void all_of_examples() {
using std::begin;
using std::end;
//C++17
//template< class InputIt, class UnaryPredicate >
//constexpr bool all_of( InputIt first, InputIt last, UnaryPredicate p );
constexpr auto v2 = std::array<int, 4>{1, 1, 1, 1};
constexpr auto eqOne = [](int x) constexpr {
constexpr int one = 1;
return x == one;
};
constexpr bool isAllV2 = std::all_of(begin(v2), end(v2), eqOne);
std::cout << isAllV2 << std::flush << '\n';
}
gcc-8 issues a diagnostic(clang issues a similar error as well):
examples.cpp:21:41: error: call to non-'constexpr' function 'bool std::all_of(_IIter, _IIter, _Predicate) [with _IIter = const int*; _Predicate = algo::all_of_examples()::]'
constexpr bool isAllV2 = std::all_of(begin(v2), end(v2), eqOne);
I know that lamdas since C++17 can be constexpr and also std::begin && std::end are both marked as constexpr. Furthermore, std::array can also be constexpr. Therefore, why the compiler doesn't pick the constexpr version of std::all_of and complains that it isn't?
std::all_of is only constexpr as of c++20. Also libstdc++ doesn't support this yet.
libc++ has support as of llvm-7 but even when using clang you probably still use libstdc++.
See:
https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/stl_algo.h#L508
https://github.com/llvm-mirror/libcxx/blob/release_70/include/algorithm#L925
Related
I'm writing Boost.Asio style async function that takes CompletionToken argument.
The argument can be function, function object, lambda expression, future, awaitable, etc.
CompletionToken is template parameter. If I don't restrict the argument, the function could match unexpected parameter such as int. So I want to write some restriction using std::enable_if. (My environment is C++17).
If CompletionToken takes parameters, then I can use std::is_invocable for checking.
See f1 in my example code.
However, I got a problem. If CompletionToken takes no parameters, then boost::asio::use_future makes an error.
See f2 in my example code.
After some of try and error, I got my solution. That is concatenating std::is_invocable and use_future_t checking by OR (||).
See f3 in my example code.
But it is not so elegant. In addition, I'm not sure other features supported by Boost.Asio e.g.) use_awaitable_t requires similar direct matching check.
I tried to find Boost.Asio provides type traits or predicate such as is_completion_token, but I couldn't find it.
Is there any better way to checking CompletionToken?
Godbolt link https://godbolt.org/z/sPeMo1GEK
Complete Code:
#include <type_traits>
#include <boost/asio.hpp>
// Callable T takes one argument
template <
typename T,
std::enable_if_t<std::is_invocable_v<T, int>>* = nullptr
>
void f1(T) {
}
// Callable T takes no argument
template <
typename T,
std::enable_if_t<std::is_invocable_v<T>>* = nullptr
>
void f2(T) {
}
template <template <typename...> typename, typename>
struct is_instance_of : std::false_type {};
template <template <typename...> typename T, typename U>
struct is_instance_of<T, T<U>> : std::true_type {};
// Callable T takes no argument
template <
typename T,
std::enable_if_t<
std::is_invocable_v<T> ||
is_instance_of<boost::asio::use_future_t, T>::value
>* = nullptr
>
void f3(T) {
}
int main() {
// no error
f1([](int){});
f1(boost::asio::use_future);
// same rule as f1 but use_future got compile error
f2([](){});
f2(boost::asio::use_future); // error
// a little complecated typechecking, then no error
f3([](){});
f3(boost::asio::use_future);
}
Outputs:
Output of x86-64 clang 13.0.1 (Compiler #1)
<source>:45:5: error: no matching function for call to 'f2'
f2(boost::asio::use_future); // error
^~
<source>:17:6: note: candidate template ignored: requirement 'std::is_invocable_v<boost::asio::use_future_t<std::allocator<void>>>' was not satisfied [with T = boost::asio::use_future_t<>]
void f2(T) {
^
1 error generated.
If you have c++20 concepts, look below. Otherwise, read on.
When you want to correctly implement the async-result protocol using Asio, you would use the async_result trait, or the async_initiate as documented here.
This should be a reliable key for SFINAE. The template arguments to async_result include the token and the completion signature(s):
Live On Compiler Explorer
#include <boost/asio.hpp>
#include <iostream>
using boost::asio::async_result;
template <typename Token,
typename R = typename async_result<std::decay_t<Token>, void(int)>::return_type>
void f1(Token&&) {
std::cout << __PRETTY_FUNCTION__ << "\n";
}
template <typename Token,
typename R = typename async_result<std::decay_t<Token>, void()>::return_type>
void f2(Token&&) {
std::cout << __PRETTY_FUNCTION__ << "\n";
}
int main() {
auto cb1 = [](int) {};
f1(cb1);
f1(boost::asio::use_future);
f1(boost::asio::use_awaitable);
f1(boost::asio::detached);
f1(boost::asio::as_tuple(boost::asio::use_awaitable));
auto cb2 = []() {};
f2(cb2);
f2(boost::asio::use_future);
f2(boost::asio::use_awaitable);
f2(boost::asio::detached);
f2(boost::asio::as_tuple(boost::asio::use_awaitable));
}
Already prints
void f1(Token&&) [with Token = main()::<lambda(int)>&; R = void]
void f1(Token&&) [with Token = const boost::asio::use_future_t<>&; R = std::future<int>]
void f1(Token&&) [with Token = const boost::asio::use_awaitable_t<>&; R = boost::asio::awaitable<int, boost::asio::any_io_executor>]
void f1(Token&&) [with Token = const boost::asio::detached_t&; R = void]
void f1(Token&&) [with Token = boost::asio::as_tuple_t<boost::asio::use_awaitable_t<> >; R = boost::asio::awaitable<std::tuple<int>, boost::asio::any_io_executor>]
void f2(Token&&) [with Token = main()::<lambda()>&; R = void]
void f2(Token&&) [with Token = const boost::asio::use_future_t<>&; R = std::future<void>]
void f2(Token&&) [with Token = const boost::asio::use_awaitable_t<>&; R = boost::asio::awaitable<void, boost::asio::any_io_executor>]
void f2(Token&&) [with Token = const boost::asio::detached_t&; R = void]
void f2(Token&&) [with Token = boost::asio::as_tuple_t<boost::asio::use_awaitable_t<> >; R = boost::asio::awaitable<std::tuple<>, boost::asio::any_io_executor>]
C++20 Concepts
Now keep in mind the above is "too lax" due to partial template instantiation. Some parts of async_result aren't actually used. That means that f2(cb1); will actually compile.
The linked docs even include the C++20 completion_token_for<Sig> concept that allows you to be precise at no effort: Live On Compiler Explorer
template <boost::asio::completion_token_for<void(int)> Token> void f1(Token&&) {
std::cout << __PRETTY_FUNCTION__ << "\n";
}
template <boost::asio::completion_token_for<void()> Token> void f2(Token&&) {
std::cout << __PRETTY_FUNCTION__ << "\n";
}
Otherwise
In practice you would always follow the Asio recipe, and that guarantees that all parts are used. Apart from the example in the documentation, you can search existing answers
Example:
template <typename Token>
typename asio::async_result<std::decay_t<Token>, void(error_code, int)>::return_type
async_f1(Token&& token) {
auto init = [](auto completion) {
auto timer =
std::make_shared<asio::steady_timer>(boost::asio::system_executor{}, 1s);
std::thread(
[timer](auto completion) {
error_code ec;
timer->wait(ec);
std::move(completion)(ec, 42);
},
std::move(completion))
.detach();
};
return asio::async_result<std::decay_t<Token>, void(error_code, int)>::initiate(
init, std::forward<Token>(token));
}
Consider the following uncomplicated code:
#include <thread>
#include <utility>
#include <vector>
#include <atomic>
#include <queue>
#include <iostream>
#include <functional>
using namespace std;
template<class It, class Fun>
void parallel_for(size_t num_threads, It first, It end, const Fun& fun) {
std::queue<std::thread> ts;
for (It it = first; it != end; ++it) {
if (std::distance(first, it) % num_threads == 0) {
fun(*it);
} else {
if (ts.size() == num_threads-1) {
ts.front().join();
ts.pop();
}
ts.push(std::thread(fun, std::ref(*it)));
}
}
while (not ts.empty()) {
ts.front().join();
ts.pop();
}
}
int main() {
std::atomic_int counter = 1;
auto lam = [&counter](auto& vl) {
vl = std::pair(counter++, -1);
};
// The following usage of std::ref works okay:
pair<int, int> x;
auto blam = bind(lam, ref(x));
blam();
// Nevertheless, the next line fails:
// lam(ref(x));
// As well as the next two ones:
// vector<pair<int, int>> v = {{4, 2}};
// parallel_for(thread::hardware_concurrency(), begin(v), end(v), lam);
return 0;
}
GCC's error on the last two lines, in particular, is
In file included from ./src/csc_cpp/passing_lambdas.cpp:1:
/usr/include/c++/10/thread: In instantiation of ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = const main()::<lambda(auto:1&)>&; _Args = {std::reference_wrapper<std::pair<int, int> >}; <template-parameter-1-3> = void]’:
./src/csc_cpp/passing_lambdas.cpp:22:26: required from ‘void parallel_for(size_t, It, It, const Fun&) [with It = __gnu_cxx::__normal_iterator<std::pair<int, int>*, std::vector<std::pair<int, int> > >; Fun = main()::<lambda(auto:1&)>; size_t = long unsigned int]’
./src/csc_cpp/passing_lambdas.cpp:47:71: required from here
/usr/include/c++/10/thread:136:44: error: static assertion failed: std::thread arguments must be invocable after conversion to rvalues
136 | typename decay<_Args>::type...>::value,
| ^~~~~
I am sure this is a trivial matter, but I am anyway struggling to understand this. I think I have been following the available examples on std::thread::thread()'s intended use quite closely, but this does not compile. What am I doing wrong?
First, let me clarify, because I'm not sure if it's obvious: the trick behind std::ref is that it returns an object of type std::reference_wrapper<T>, so you can use the result as object, but the object is implicitly convertible to T&, so it can be substituted where T& is needed.
lam(ref(x)); fails because you use auto in lam. Compiler doesn't know that you want vl to be std::pair<int, int>&, it deduces from what it gets. std::ref returns a temporary of std::reference_wrapper<std::pair<int, int>>, which cannot be bound to non-const reference. Use explicit type in lambda and it compiles:
auto lam = [&counter](std::pair<int, int>& vl) {
vl = std::pair(counter++, -1);
};
lam(std::ref(x));
Alternatively, you can explicitly convert to std::pair<int, int>& using get() or static_cast
auto lam = [&counter](auto& vl) {
vl = std::pair(counter++, -1);
};
lam(std::ref(x).get());
lam(static_cast<std::pair<int, int>&>(std::ref(x)));
The second part with parallel_for has exactly the same issue, you pass rvalue of std::reference_wrapper to lam.
About
lam(ref(x));
x is an lvalue while the ref(x) is a temporary reference_wrapper. You can not grab a temporary with an lvalue reference in your lam through auto&.
for that line, you can simply use
lam(x);
I tried to use std::reduce with a functor to calculate the number of characters in an array. GCC gives an error, while it compiles and works in MSVC. link here
#include <iostream>
#include <array>
#include <numeric>
#include <cstring>
int main()
{
std::array arr{ "Mickey","Minnie","Jerry" };
struct StringLength
{
auto operator()(const char* l, size_t r)
{
return strlen(l) + r;
}
auto operator()(size_t l, const char* r)
{
return l + strlen(r);
}
auto operator()(const char* l, const char* r)
{
return strlen(l) + strlen(r);
}
auto operator()(size_t l, size_t r)
{
return l + r;
}
};
std::cout << std::reduce(arr.begin(), arr.end(), size_t{}, StringLength());
// this ^ works in MSVC
}
GCC 10.1 error because important information should not be hidden behind a link:
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/numeric:
In instantiation of '_Tp std::reduce(_InputIterator, _InputIterator, _Tp, _BinaryOperation)
[with _InputIterator = const char**; _Tp = long unsigned int;
_BinaryOperation = main()::StringLength]':
<source>:29:78: required from here
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/numeric:263:21: error:
static assertion failed
263 | static_assert(is_convertible_v<value_type, _Tp>);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I agree with dewaffled that this is a bug. The libstdc++ implementation of std::reduce looks like this:
template<typename InputIt, typename Tp, typename BinOp>
Tp reduce(InputIt first, InputIt last, Tp init, BinOp binary_op) {
using value_type = typename iterator_traits<InputIt>::value_type;
static_assert(is_invocable_r_v<Tp, BinOp&, Tp&, Tp&>);
static_assert(is_convertible_v<value_type, Tp>);
// ...
}
I wasn't able to find a requirement in the standard that iterator's value_type has to be convertible into Tp. Moreover, this requirement is not necessary at all. If you remove that static assert, your code will compile just fine as it should.
Update from GCC Bugzilla
Fixed for 9.5, 10.4 and 11.2.
Jonathan Wakely, 2021-06-18
I simplified my code producing an error, and found that even this simple counting function gave me an error (see below):
#include <boost/hana/tuple.hpp>
#include <boost/hana/fold.hpp>
#include <boost/hana/plus.hpp>
#include <boost/hana/integral_constant.hpp>
int main() {
using namespace boost;
constexpr auto inc = [](auto n, auto el) { return hana::int_c<n> + hana::int_c<1>; };
constexpr auto count = hana::fold(hana::make_tuple(hana::int_c<3>),
hana::int_<0>{},
inc
);
return 0;
}
Error (ommitted some that seemed irrelevant):
/usr/local/include/boost/hana/detail/variadic/foldl1.hpp:202:57: error: ‘static constexpr decltype(auto) boost::hana::detail::variadic::foldl1_impl<2u>::apply(F&&, X1&&, X2&&) [with F = const main()::<lambda(auto:1, auto:2)>&; X1 = boost::hana::integral_constant<int, 0>; X2 = boost::hana::integral_constant<int, 3>]’ called in a constant expression
return foldl1_impl<sizeof...(xn) + 1>::apply(
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
static_cast<F&&>(f), static_cast<X1&&>(x1), static_cast<Xn&&>(xn)...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
);
~
In file included from /usr/local/include/boost/hana/fold_left.hpp:18:0,
from /usr/local/include/boost/hana/concept/foldable.hpp:19,
from /usr/local/include/boost/hana/core/to.hpp:16,
from /usr/local/include/boost/hana/bool.hpp:17,
from /usr/local/include/boost/hana/tuple.hpp:16,
from ...:
/usr/local/include/boost/hana/detail/variadic/foldl1.hpp:31:41: note: ‘static constexpr decltype(auto) boost::hana::detail::variadic::foldl1_impl<2u>::apply(F&&, X1&&, X2&&) [with F = const main()::<lambda(auto:1, auto:2)>&; X1 = boost::hana::integral_constant<int, 0>; X2 = boost::hana::integral_constant<int, 3>]’ is not usable as a constexpr function because:
static constexpr decltype(auto) apply(F&& f, X1&& x1, X2&& x2) {
^~~~~
/usr/local/include/boost/hana/detail/variadic/foldl1.hpp:32:39: error: call to non-constexpr function ‘main()::<lambda(auto:1, auto:2)> [with auto:1 = boost::hana::integral_constant<int, 0>; auto:2 = boost::hana::integral_constant<int, 3>]’
return static_cast<F&&>(f)(static_cast<X1&&>(x1),
~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
static_cast<X2&&>(x2));
I used c++17 with g++ 6.3. It looks like it says that the lambda isn't seen as a constexpr, but it looks to me like it uses only constant values. Can anyone suggest to me how I can make this code work? (It's purpose is to count the number of elements in the tuple passed to fold)
If your compiler doesn't support constexpr lambdas, you have to write out the closure types yourself (at namespace or class scope, not at function scope since a local class cannot have member templates):
constexpr struct inc_t {
template<class N, class T>
constexpr auto operator()(N n, T) const { return hana::int_c<n> + hana::int_c<1>; }
} inc;
Otherwise, you can still use hana but the result of computations will not be available to the type system.
A code snippet says more than a couple of paragraphs:
#include <boost/hana/fwd/eval_if.hpp>
#include <boost/hana/core/is_a.hpp>
#include <iostream>
#include <functional>
using namespace boost::hana;
template<class arg_t>
decltype(auto) f2(arg_t const& a)
{
constexpr bool b = is_a<std::reference_wrapper<std::string>,
arg_t>;
auto wrapper_case = [&a](auto _) -> std::string&
{ return _(a).get(); };
auto default_case = [&a]() -> arg_t const&
{ return a; };
return eval_if(b, wrapper_case, default_case);
}
int main()
{
int a = 3;
std::string str = "hi!";
auto str_ref = std::ref(str);
std::cout << f2(a) << ", " << f2(str) << ", " << f2(str_ref)
<< std::endl;
}
The compiler error is:
$> g++ -std=c++14 main.cpp
main.cpp: In instantiation of ‘decltype(auto) f2(const arg_t&) [with arg_t = int]’:
main.cpp:42:22: required from here
main.cpp:31:19: error: use of ‘constexpr decltype(auto) boost::hana::eval_if_t::operator()(Cond&&, Then&&, Else&&) const [with Cond = const bool&; Then = f2(const arg_t&) [with arg_t = int]::<lambda(auto:1)>&; Else = f2(const arg_t&) [with arg_t = int]::<lambda(auto:2)>&]’ before deduction of ‘auto’
return eval_if(b, wrapper_case, default_case);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
There's no recursion, I use gcc-6.0.2 which presumably has solved some bugs about decltype(auto) and has a full working C++14 implementation, and also fits the boost::hana requirements, so, my error must be in my implementation but I don't know what is the error about.
NOTE: clang++ 3.8.0 throws a similar compiler error.
First, in case the path doesn't make it clear, boost/hana/fwd/eval_if.hpp is but a forward declaration. If you want to use it in earnest, you need to include the whole thing, i.e., boost/hana/eval_if.hpp. That's the cause of the original error.
Then, this bool is wrong:
constexpr bool b = is_a<std::reference_wrapper<std::string>,
arg_t>;
Coercing it to bool means that the type no longer carries the value information. Use auto.