检查函数的概念不适用于仅可移动的参数
Concept checking a function doesn't work with movable-only arguments
我有一个调用回调函数的函数,该回调函数接受仅可移动类型(例如 unique_ptr
)。
template <typename Function>
void foo(const Function& function) {
BOOST_CONCEPT_ASSERT((
boost::UnaryFunction<Function, void, std::unique_ptr<Bar>));
auto bar = std::make_unique<Bar>();
...
function(std::move(bar));
}
尝试编译此代码时,我收到一条消息,指出 BOOST_CONCEPT_ASSERT
行试图复制 unique_ptr
。如果我删除该行,代码可以正常工作。 Boost.Concept 库似乎不支持移动语义。如果不编写我自己的概念 class(顺便说一句,支持左值和右值作为它们的参数并不是很简单),是否有任何解决方法。
#include <type_traits>
#include <utility>
template <typename...>
struct voider { using type = void; };
template <typename... Ts>
using void_t = typename voider<Ts...>::type;
template <typename, typename = void_t<>>
struct is_callable : std::false_type {};
template <typename F, typename... Args>
struct is_callable<F(Args...), void_t<decltype(std::declval<F>()(std::declval<Args>()...))>> : std::true_type {};
//...
static_assert(is_callable<Function&(std::unique_ptr<Bar>)>{}, "Not callable");
没错。不幸的是,UnaryFunction
作为一个概念写成:
BOOST_concept(UnaryFunction,(Func)(Return)(Arg))
{
BOOST_CONCEPT_USAGE(UnaryFunction) { test(is_void<Return>()); }
private:
void test(boost::mpl::false_)
{
f(arg); // "priming the pump" this way keeps msvc6 happy (ICE)
Return r = f(arg);
ignore_unused_variable_warning(r);
}
void test(boost::mpl::true_)
{
f(arg); // <== would have to have std::move(arg)
// here to work, or at least some kind of
// check against copy-constructibility, etc.
}
#if (BOOST_WORKAROUND(__GNUC__, BOOST_TESTED_AT(4) \
&& BOOST_WORKAROUND(__GNUC__, > 3)))
// Declare a dummy construktor to make gcc happy.
// It seems the compiler can not generate a sensible constructor when this is instantiated with a refence type.
// (warning: non-static reference "const double& boost::UnaryFunction<YourClassHere>::arg"
// in class without a constructor [-Wuninitialized])
UnaryFunction();
#endif
Func f;
Arg arg;
};
由于 arg
是通过左值传递的,因此无法使其与 Boost.Concepts 一起使用。直接地。不过,您 可以 编写 hack。由于我们只是调用检查 f(arg)
是否有效,我们可以为 arg
构造一个可转换为 unique_ptr<Bar>
的本地类型。即:
template <typename Function>
void foo(Function f)
{
struct Foo {
operator std::unique_ptr<int>();
};
BOOST_CONCEPT_ASSERT((
boost::UnaryFunction<Function, void, Foo>));
f(std::make_unique<int>(42));
}
或者更一般地说:
template <typename T>
struct AsRvalue {
operator T(); // no definition necessary
};
template <typename Function>
void foo(Function f)
{
BOOST_CONCEPT_ASSERT((
boost::UnaryFunction<Function, void, AsRvalue<std::unique_ptr<int>>>));
f(std::make_unique<int>(42));
}
在 gcc 和 clang 上为我编译(尽管在 clang 上给出了关于未使用的 typedef 的警告)。然而,在这一点上,只写出你自己的概念来让它发挥作用可能会更清楚。像 这样的东西是最简单的。
我有一个调用回调函数的函数,该回调函数接受仅可移动类型(例如 unique_ptr
)。
template <typename Function>
void foo(const Function& function) {
BOOST_CONCEPT_ASSERT((
boost::UnaryFunction<Function, void, std::unique_ptr<Bar>));
auto bar = std::make_unique<Bar>();
...
function(std::move(bar));
}
尝试编译此代码时,我收到一条消息,指出 BOOST_CONCEPT_ASSERT
行试图复制 unique_ptr
。如果我删除该行,代码可以正常工作。 Boost.Concept 库似乎不支持移动语义。如果不编写我自己的概念 class(顺便说一句,支持左值和右值作为它们的参数并不是很简单),是否有任何解决方法。
#include <type_traits>
#include <utility>
template <typename...>
struct voider { using type = void; };
template <typename... Ts>
using void_t = typename voider<Ts...>::type;
template <typename, typename = void_t<>>
struct is_callable : std::false_type {};
template <typename F, typename... Args>
struct is_callable<F(Args...), void_t<decltype(std::declval<F>()(std::declval<Args>()...))>> : std::true_type {};
//...
static_assert(is_callable<Function&(std::unique_ptr<Bar>)>{}, "Not callable");
没错。不幸的是,UnaryFunction
作为一个概念写成:
BOOST_concept(UnaryFunction,(Func)(Return)(Arg))
{
BOOST_CONCEPT_USAGE(UnaryFunction) { test(is_void<Return>()); }
private:
void test(boost::mpl::false_)
{
f(arg); // "priming the pump" this way keeps msvc6 happy (ICE)
Return r = f(arg);
ignore_unused_variable_warning(r);
}
void test(boost::mpl::true_)
{
f(arg); // <== would have to have std::move(arg)
// here to work, or at least some kind of
// check against copy-constructibility, etc.
}
#if (BOOST_WORKAROUND(__GNUC__, BOOST_TESTED_AT(4) \
&& BOOST_WORKAROUND(__GNUC__, > 3)))
// Declare a dummy construktor to make gcc happy.
// It seems the compiler can not generate a sensible constructor when this is instantiated with a refence type.
// (warning: non-static reference "const double& boost::UnaryFunction<YourClassHere>::arg"
// in class without a constructor [-Wuninitialized])
UnaryFunction();
#endif
Func f;
Arg arg;
};
由于 arg
是通过左值传递的,因此无法使其与 Boost.Concepts 一起使用。直接地。不过,您 可以 编写 hack。由于我们只是调用检查 f(arg)
是否有效,我们可以为 arg
构造一个可转换为 unique_ptr<Bar>
的本地类型。即:
template <typename Function>
void foo(Function f)
{
struct Foo {
operator std::unique_ptr<int>();
};
BOOST_CONCEPT_ASSERT((
boost::UnaryFunction<Function, void, Foo>));
f(std::make_unique<int>(42));
}
或者更一般地说:
template <typename T>
struct AsRvalue {
operator T(); // no definition necessary
};
template <typename Function>
void foo(Function f)
{
BOOST_CONCEPT_ASSERT((
boost::UnaryFunction<Function, void, AsRvalue<std::unique_ptr<int>>>));
f(std::make_unique<int>(42));
}
在 gcc 和 clang 上为我编译(尽管在 clang 上给出了关于未使用的 typedef 的警告)。然而,在这一点上,只写出你自己的概念来让它发挥作用可能会更清楚。像