Why can't I pass two bool value into std::function? - c++11

I want to define a callback by using std::function, the definition like below:
typedef std::function<void (const std::string &, const std::string &,
const bool, const bool)> Callback;
But it has a syntax error, what can I do to fix this issue?

You should include functional, to use function:
#include <functional>

Related

g++ fails to look up static functions in a template class, is it a bug or standard defined?

When I try some C++11 code like following, it passed in all clang++ available to me that support C++11, but it failed to compile in g++-4.8, g++-4.9 and g++-5.0.
#include <type_traits>
#include <vector>
template <class C, class First, class Last>
struct HasInsertEnd {
template <class U>
static std::false_type Check(...);
template <class U>
static auto Check(U val)
-> decltype(val.insert(val.end(), std::declval<First>(),
std::declval<Last>()),
std::true_type{});
template <class U>
using Deduce = decltype(Check<U>(std::declval<U>()));
using type = typename Deduce<C>::type;
static constexpr bool value = type::value;
};
int main(int argc, char* argv[]) {
static_assert(!HasInsertEnd<int, int, int>::value, "...");
static_assert(HasInsertEnd<std::vector<int>, const int*, const int*>::value,
"...");
return 0;
}
g++ will report errors like:
‘Check’ was not declared in this scope
If I change the calling of Check in the Deduce to HasInsertEnd::Check, both g++ and clang++ will be happy.
I know little about dependent name lookup. The problem is, which behavior is standard?
This is a bug in GCC, and can be shown to be a bug in GCC even without deferring to the standard.
template <typename T>
struct f { typedef int type; };
template <typename T>
struct S {
template <typename U>
static f<U> f();
template <class U>
using u = typename decltype(f<U>())::type;
using t = u<T>;
};
S<int>::t main() { }
This is rejected the same way in GCC 4.7.4 and GCC 5, with "error: ‘f’ was not declared in this scope". That's just nonsense. Even if the static member function should somehow not be visible, there is still a global type by the same name that would be found instead. It gets even better, though: with that global type, you get:
test.cc: In substitution of ‘template<class T> template<class U> using u = typename decltype (f<U>())::type [with U = T; T = T]’:
test.cc:12:20: required from here
test.cc:10:36: error: ‘f’ was not declared in this scope
using u = typename decltype(f<U>())::type;
^
test.cc:10:36: note: suggested alternative:
test.cc:2:12: note: ‘f’
struct f { typedef int type; };
^
test.cc:15:13: error: ‘t’ in ‘struct S<int>’ does not name a type
S<int>::t main() { }
^
That's right, it's suggesting that f can be corrected by spelling it f.
I don't see any problem with your code, and if it isn't a known bug, I encourage you to report it. and it's been reported as a bug before.
Oddly, as noted in the comments, GCC 4.8.4 and GCC 4.9.2 do find a global f. However, if the global f is a type, then they still reject the program, with "error: missing template arguments" even though the template argument is provided, so it's still clearly a bug in GCC.

Second scope after struct definition

I looked a bit into Eric Nieblers range library https://github.com/ericniebler/range-v3/ and there (/include/range/v3/utility/concepts.hpp, line 36) I found code of the form
constexpr struct valid_expr_t
{
template<typename ...T>
true_ operator()(T &&...) const;
} valid_expr {};
I am confused to the second scope/braces after valid_expr. What is the meaning of the whole construct. Is this even a struct definition? The syntax seems not allowed in C++98. What can go into these second pair of braces?
It's the C++11 uniform initialization syntax, and it simply initializes the valid_expr object.
It's like doing
struct valid_expr_t
{
template<typename ...T>
true_ operator()(T &&...) const;
};
constexpr valid_expr_t valid_expr {};

Length of user-defined string literal as a template argument?

Is there any way to get behavior like this?
// Some definition(s) of operator "" _my_str
// Some definition of function or macro MY_STR_LEN
using T1 = MY_STR_LEN("ape"_my_str);
// T1 is std::integral_constant<std::size_t, 3U>.
using T2 = MY_STR_LEN("aardvark"_my_str);
// T2 is std::integral_constant<std::size_t, 8U>.
It seems not, since the string literals are passed immediately to some_return_type operator "" _my_str(const char*, std::size_t); and never to a literal operator template (2.14.8/5). That size function parameter can't be used as a template argument, even though it will almost always be a constant expression.
But it seems like there ought to be some way to do this.
Update: The accepted answer, that this is not possible without an extra definition per literal, is accurate for C++11 as asked, and also C++14 and C++17. C++20 allows the exact result asked for:
#include <cstdlib>
#include <type_traits>
#include <string_view>
struct cexpr_str {
const char* ptr;
std::size_t len;
template <std::size_t Len>
constexpr cexpr_str(const char (&str)[Len]) noexcept
: ptr(str), len(Len) {}
};
// Essentially the same as
// std::literals::string_view_literals::operator""sv :
template <cexpr_str Str>
constexpr std::string_view operator "" _my_str () noexcept
{
return std::string_view(Str.ptr, Str.len);
}
#define MY_STR_LEN(sv) \
std::integral_constant<std::size_t, (sv).size()>
Reading C++11 2.14.8 carefully reveals that the "literal operator template" is only considered for numeric literals, but not for string and character literals.
However, the following approach seems to give you constexpr access to the string length (but not the pointer):
struct MyStr
{
char const * str;
unsigned int len;
constexpr MyStr(char const * p, unsigned int n) : str(p), len(n) {}
};
constexpr MyStr operator "" _xyz (char const * s, unsigned int len)
{
return MyStr(s, len);
}
constexpr auto s = "Hello"_xyz;
Test:
#include <array>
using atype = std::array<int, s.len>; // OK

static_assert in a function declaration

I've got quite a simple function using static_assert. The trouble is that I want to static_assert on behaviour involved in the function declaration- inferring the return type, specifically. There don't seem to be any places to interject the static_assert so that I can fire it before the compiler fails to deduce the return type.
So far, I put return type deduction and the static assertion in a struct. This will fire the assertion, which is great, but it'll still generate an error on the type deduction, which is noise I want to eliminate.
#include <type_traits>
#include <functional>
#include <memory>
#include <map>
#include <iostream>
#include <string>
#include <cstdio>
#include <tuple>
#include <sstream>
#include <vector>
#include <algorithm>
template<typename T, typename X> struct is_addable {
template<typename Test, typename Test2> static char test(decltype(*static_cast<Test*>(nullptr) + *static_cast<Test2*>(nullptr))*);
template<typename Test, typename Test2> static int test(...);
static const bool value = std::is_same<char, decltype(test<T, X>(nullptr))>::value;
};
template<typename T, typename X> struct is_addable_fail {
static const bool value = is_addable<T, X>::value;
static_assert(value, "Must be addable!");
typedef decltype(*static_cast<T*>(nullptr) + *static_cast<X*>(nullptr)) lvalue_type;
};
template<typename T1, typename T2> auto Add(T1&& t1, T2&& t2) -> typename is_addable_fail<T1, T2>::lvalue_type {
return std::forward<T1>(t1) + std::forward<T2>(t2);
}
struct f {};
int main() {
std::cout << Add(std::string("Hello"), std::string(" world!"));
Add(f(), f());
}
It's not possible because of the way candidate sets are built and SFINAE. If you could assert before a function's signature has fully been determined then that would require you to assert before it has been decided that the function is the one that is going to be used.
The order of steps is essentially:
Find matching functions
Substitute deduced parameters into the function arguments and return type.
Discard those that fail (SFINAE)
If one left, use that.
When do you want the assert to fire?
If you fire it during parameter substitution then you are ruling out SFINAE, and if you fire it any time after that then the return type has already been determined (too late).
Though I may misunderstand the question,
does SFINAE like the following meet the purpose?
template<typename T = int> void Add(...) {
static_assert(sizeof(T) == 0, "Must be addable!");
}
template<typename T1, typename T2> auto Add(T1&& t1, T2&& t2) ->
decltype(std::forward<T1>(t1) + std::forward<T2>(t2)) {
return std::forward<T1>(t1) + std::forward<T2>(t2);
}
Here is a test on ideone.
The shortcoming is that Add has to be repeated.
EDIT:
Though I'm not totally sure this is strictly standard conforming, does the
following work-around help?
(a test on ideone)
template<typename T1, typename T2> void Add(T1 volatile&&, T2 volatile&&) {
static_assert(sizeof(T1) == 0, "Must be addable!");
}

How to use std::string with asio::buffer()

I get the following error message when I'm trying to use std::string with boost::asio::buffer:
boost/asio/detail/consuming_buffers.hpp:
In constructor
'boost::asio::detail::consuming_buffers<
boost::asio::mutable_buffer, boost::asio::const_buffers_1
>::consuming_buffers(const boost::asio::const_buffers_1 &)':
boost/asio/impl/read.hpp:140:25:
instantiated from
'boost::asio::detail::read_op<
boost::asio::basic_stream_socket<boost::asio::ip::tcp>,
boost::asio::const_buffers_1
, boost::asio::detail::transfer_all_t
, boost::_bi::bind_t<
void, boost::_mfi::mf1<void, read_op, const
boost::system::error_code &>
, boost::_bi::list2<boost::_bi::value<read_op
*>, boost::arg<1> (*)()>
>
>::read_op(
boost::asio::basic_stream_socket<boost::asio::ip::tcp>
&, const boost::asio::const_buffers_1
&
, boost::asio::detail::transfer_all_t
, boost::_bi::bind_t<
void, boost::_mfi::mf1<void, read_op, const
boost::system::error_code &>
, boost::_bi::list2<boost::_bi::value<read_op
*>, boost::arg<1> (*)()>
>
)'
[...]
Full source code: http://liveworkspace.org/code/eca749f6f2714b7c3c4df9f26a404d86
I think the problem is that you are passing a const buffer to async_read instead of a mutable buffer. In the block ending in line 50, boost::asio::buffer(_header) returns a const buffer. You should do something like boost::asio::async_read(s, boost::asio::buffer(data, size), handler), because boost::asio::buffer(data, size) creates a mutable buffer.
Instead of using std::strings for _header and _data, you probably need to use arrays of char, such as:
char* _data;
boost::asio::buffer(_data, strlen(_data));
See reference documentations for buffer and async_read.
You must pass a pointer as the first parameter:
#include <string>
#include <boost/asio.hpp>
std::string request, reply;
auto rsize = boost::asio::buffer(&reply[0], request.size());
From the boost::asio::buffer reference documentation:
It seems that std::string could only be passed into an asio::buffer as a const reference.
std::vector<char> should be a better alternative:
std::vector<char> d2(128);
bytes_transferred = sock.receive(boost::asio::buffer(d2));

Resources