我正在学习conceptsC++ 20,并提出需要concept使用先前定义的concept.
因此,在下面的示例中,我预计f<pub_b>()会生成编译器错误,因为events_publishedinpub_b不满足publisher概念,因为ev_2不满足event概念。
看来在概念event中使用概念publisher并没有什么效果。
g++ version报告
g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
#include <concepts>
#include <tuple>
template <typename t>
concept event = requires {
std::default_initializable<t>;
std::copy_constructible<t>;
std::move_constructible<t>; …Run Code Online (Sandbox Code Playgroud) 给定一个可变参数模板,我想定义一个对象,该对象是可变参数模板中每种类型的向量元组。
就像是:
template <typename... Ts>
class C {
std::tuple<std::vector<T1>, std::vector<T2>,...,std::vector<Tn>> vectors;
};
Run Code Online (Sandbox Code Playgroud)
它不一定是一个数据成员,我的意思是,如果可以使用更多辅助类型或对象来完成此操作,那将不是问题。
std::make_index_sequence我尝试了与的组合std::tuple,但我无法弄清楚。
c++ metaprogramming template-meta-programming variadic-templates stdtuple
我想做这样的事情:
#include <iostream>
class a {
public:
a() : i(2) {}
template <typename ...ts>
void exec() {
f<ts...>();
std::cout << "a::()" << std::endl;
}
int i;
private:
template <typename t>
void f() {
i += t::i;
}
template <typename t, typename ...ts>
void f() {
f<t>();
f<t, ts...>();
}
};
struct b {
static const int i = -9;
};
struct c {
static const int i = 4;
};
int main()
{
a _a;
_a.exec<b,c>();
std::cout << _a.i << …Run Code Online (Sandbox Code Playgroud) 标头中cnvt.h有:
template <typename t_to, typename t_from> std::optional<t_to> cnvt(t_from);
Run Code Online (Sandbox Code Playgroud)
在 header 中int.h,我们有:
#include "cnvt.h"
template <>
std::optional<uint8_t> cnvt<uint8_t, const char *>(const char *) {
// actual code replaced for simplicity
return {};
}
Run Code Online (Sandbox Code Playgroud)
clang-tidy报道称,事实如此
clang 报告“int.h:: 不直接使用包含的标头 cvnt.h(可用修复) 不直接使用包含的标头 cvnt.h(可用修复)
如果删除#include "cnvt.h",它会报告
int.h:9:24:没有函数模板与函数模板专业化“cnvt”匹配
我搜索了 StackOverflow,关于函数模板专业化的帖子没有帮助。
My goal is to have a generic way to traverse a std::tuple, and the following code tries to show it:
#include <iostream>
#include <tuple>
template <typename t, size_t t_idx, typename t_tuple>
concept visit_tuple_element_value = requires(t &&p_t, const t_tuple &p_tuple) {
{
p_t.template operator()<t_idx>(
std::declval<std::add_const_t<std::add_lvalue_reference_t<t_tuple>>>())
} -> std::same_as<bool>;
};
template <typename t_tuple, typename t_function, size_t t_idx = 0>
requires(visit_tuple_element_value<t_function, t_idx, t_tuple>)
void traverse_tuple_values(t_function p_function, const t_tuple &p_tuple) {
if constexpr (t_idx < std::tuple_size_v<t_tuple>) {
if (p_function.template operator()<t_idx>(p_tuple)) {
traverse_tuple_values<t_tuple, t_function, …Run Code Online (Sandbox Code Playgroud) 我想使用std::string(size_type count,CharT ch)具有较大价值的count。阅读https://en.cppreference.com/w/cpp/string/basic_string/basic_string,如果此构造函数失败,我将找不到异常定义。
如果它是正确的,尽管noexcept构造函数中没有子句,如何确定该字符串已创建?我应该检查它的大小是否不为0?
在下面的示例中,为什么第 20 行会导致第 27 行到第 30 行描述的错误?
在第 33 行调用exec1效果很好。
#include <cstdint>
#include <functional>
#include <iostream>
#include <tuple>
#include <type_traits>
template <typename... t_fields>
void exec0(std::function<std::tuple<t_fields...>()> generate,
std::function<void(t_fields &&...)> handle) {
std::tuple<t_fields...> _tuple{generate()};
std::apply(handle, std::move(_tuple));
}
template <typename t_generate, typename t_function>
void exec1(t_generate generate, t_function handle) {
auto _tuple{generate()};
std::apply(handle, std::move(_tuple));
}
int main() {
auto _h = [](uint32_t u) -> void { std::cout << "u = " << u << '\n'; };
auto _g = []() -> std::tuple<uint32_t> …Run Code Online (Sandbox Code Playgroud) c++ variadic-templates std-function template-argument-deduction stdapply
我无法从int到double到 工作进行简单的转换
#include <iostream>
int main()
{
int i = 8;
double d1 = i;
double d2 = static_cast<double>(i);
std::cout << "i = " << i << ", d1 = " << d1 << ", d2 = " << d2 << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
d1和d2equals 8,而不是8.0,不仅在stdout,而且在 QtCreator 的调试器中。
AFAIK,d1应该d2是8.0,那么不应该吗?
谁能告诉我我做错了什么?
我正在设置 g++ 编译器,版本 7.4.0,符合 C++11。
谢谢!
c++ ×8
c++-concepts ×2
c++20 ×1
casting ×1
function ×1
std-function ×1
stdapply ×1
stdstring ×1
stdtuple ×1
template-argument-deduction ×1
templates ×1