一旦 lambda 捕获任何变量,以下示例就无法编译。如果删除捕获,编译器将成功编译下面的代码。
#include <concepts>
#include <functional>
#include <iostream>
template<typename T>
concept OperatorLike = requires(T t, std::string s) {
{ t[s] } -> std::same_as<std::string>;
};
template<typename T, typename O>
concept Gettable = requires(T t, O op) {
t.apply_post(0, op); };
template<std::semiregular F>
class RestApiImpl {
F m_post_method;
public:
RestApiImpl(F get = F{}) : m_post_method{std::move(get)} {}
template<OperatorLike IF>
requires std::invocable<F, const int, IF>
void apply_post(const int req, IF interface){
m_post_method(req, std::move(interface));
}
};
int main(){
int ii;
auto get = [&ii](int, …Run Code Online (Sandbox Code Playgroud) 我们如何将这个问题中的需求转换为一个概念
我已尝试以下操作:
template< typename U, typename Tin, typename Tout>
concept MyConditions =
(
U::value_type
&& Tin::value_type
&& Tout::value_type
&& std::is_floating_point_v<typename Tin::value_type>
&& std::is_integral_v<typename U::value_type>
&& std::is_floating_point_v<typename Tout::value_type>
);
Run Code Online (Sandbox Code Playgroud)
这个概念现在应用于我的成员函数之一:
class test_concept
{
template< typename U, typename Tin, typename Tout>
requires MyConditions <U, Tin, Tout>
static void test_routine(const U&, const Tin&, Tout& );
}
Run Code Online (Sandbox Code Playgroud)
测试时:
std::vector<double> test{ };
std::vector<int> testi{ };
std::vector<double> test2{ };
test_concept::test_routine(testi, test, test2);
Run Code Online (Sandbox Code Playgroud)
使用clang我收到错误消息,指出未找到匹配项,并附有一条注释:
注意:因为替换的约束表达式格式不正确:依赖类型名称 'vector<int, allocator >::value_type' U::value_type 之前缺少 'typename'
对于接下来的课程
template<typename T>
class test {
public:
using unit = std::micro;
};
Run Code Online (Sandbox Code Playgroud)
如何访问单元,而test::unit无需指定模板参数或使其成为模板别名。请注意,插入一个虚拟模板参数,例如 . int 不是一个选项,因为某些模板类无法用此类类型实例化。
我得到以下代码:
template<typename T>
concept con1 = requires(T t, std::string s){
{ t[s] } -> std::same_as<std::string>;
};
using function_signature = std::function<void ( con1 auto functor)>; // ERROR!
Run Code Online (Sandbox Code Playgroud)
虽然编译器对我直接定义 lambda 没有问题:
auto lambda_1 = [](con1 auto functor){....}
Run Code Online (Sandbox Code Playgroud)
我希望前者起作用的原因如下:
template<std::semiregular T>
class R{
T functor;
R() = default;
register_functor(T functor_) { functor = std::move(functor_);}
}
Run Code Online (Sandbox Code Playgroud)
如果我使用如下签名实例化我的类:
auto rr = R<function_signature>();
Run Code Online (Sandbox Code Playgroud)
我可以在稍后阶段注册我的函子,甚至可以在运行时更改函数,只要我保持签名相同即可。直接使用 lambda 意味着我在实例化 lambda 时会陷入困境class R
我有以下课程:
struct xyzid{
uint16_t i;
uint16_t z,y,x;
};
std::vector<xyzid> example;
(...) // fill example
uint16_t* data = reinterpret_cast<uint16_t*>(example.data());
Run Code Online (Sandbox Code Playgroud)
我现在可以确定我的指针基本上是这样的:前 16 位在移动到下一个元素之前先data引用i、然后z、y、吗?x或者,是否不能保证我的结构中的声明顺序保留在我的std::vector容器中?
以下get函数将使用可变参数返回一个字符串数组。它还通过简单地转换整数来处理提供整数的情况。我提供了这个转换函数的两个版本,一个是 lambda,另一个是自由函数。
请有人解释一下为什么使用 lambda 无法编译,而看似相同的自由函数却可以正常工作。需要进行哪些更改才能使 lambda 案例发挥作用?
#include <iostream>
#include <string>
#include <vector>
#include <ranges>
#include <format>
#include <array>
#include <type_traits>
// free function converter
template <typename KEY>
auto convert_to_string_view(const KEY& key)
{
if constexpr (std::is_integral_v<KEY> && !std::is_same_v<KEY, bool>)
return std::to_string(key);
else
return key;
}
// make an array out of varargs
template <typename T, typename... Args>
inline std::array<std::common_type_t<Args...>, sizeof...(Args)>
make_array_from_vars(const Args&... args)
requires (std::is_constructible_v<std::common_type_t<Args...>, Args> && ...)
{
return { args... };
}
template<typename... KEYS>
auto get(KEYS... …Run Code Online (Sandbox Code Playgroud) 这个简单的代码会生成一个有关“烘焙指针的对象将在完整表达式末尾被销毁”的警告。这意味着什么?entry使用后可以不使用吗get_map?还有为什么会出现这个警告
static std::map<std::string, int *> get_map() {
static std::map<std::string, int*> the_map;
return the_map;
}
int main() {
(...)
auto entry = get_map().find("HEY");
(...) use entry , is that wrong ?
}
Run Code Online (Sandbox Code Playgroud)