gpt4 book ai didi

c++模板将某种类型的成员添加到类中

转载 作者:行者123 更新时间:2023-11-30 05:07:19 28 4
gpt4 key购买 nike

我想创建一个 C++ 类,我可以在其中添加类型成员(int、long、double、string)。我对 C++ 中的模板编程非常陌生。

我一直在关注本指南:https://eli.thegreenplace.net/2014/variadic-templates-in-c/对于可变数据结构。但是我如何输入类型(int、long 等)及其值并将该成员添加到我的类中?

例如,如果这是我定义元组类的方式:

template <class... Ts> struct tuple {};

template <class T, class... Ts>
struct tuple<T, Ts...> : tuple<Ts...> {
tuple(T t, Ts... ts) : tuple<Ts...>(ts...), tail(t) {}

T tail;
};

然后我想在运行时获取输入并且:

while(cin>>type) {
cin>>value;
// add type and value to my class
}

输入可以是int 3 double 3.5 string hello .我的课应该是那种情况tuple<int, double, std::string> t(3, 3.5, "hello") .

有没有一种方法可以使用模板编程实现这一点?

最佳答案

如果您真的想使用静态类型,比如说 std::tuple<int, int, char, int>基于来自 cin 的信息然后您的编译器必须在编译时“准备”所有可能的路径。视类型数量N和最大长度 s不同的可能性,即可能性的数量随着最大长度呈指数增长s .对于非常小的Ns这可以工作。由于您可能更喜欢另一种方法(没有静态类型,比如 std::tuple<int, int, char, int>),我准备了一个仅处理类型的 C++17 示例。

#include <cstdint>
#include <iostream>
#include <tuple>

template<
class F,
class Tuple=std::tuple<>
>
auto tuplify_cin_and_call(
F f,
Tuple tuple=Tuple{}
) {
constexpr std::size_t max_tuple_size = 6;

std::cout << __PRETTY_FUNCTION__ << std::endl;

if constexpr(1 + std::tuple_size<Tuple>::value < max_tuple_size) {
std::cout << "`int`|`char` to append or `done` to finish: " << std::flush;
std::string input{};
std::cin >> input;

if(input == std::string{"int"}) {
tuplify_cin_and_call(f, std::tuple_cat(tuple, std::tuple<int>{}));
}
else if(input == std::string{"char"}) {
tuplify_cin_and_call(f, std::tuple_cat(tuple, std::tuple<char>{}));
}
else if(input == std::string{"done"}) {
return f(std::move(tuple));
}
else {
std::cout << "ERROR: invalid input" << std::endl;// `cout` or `cerr` here?
return tuplify_cin_and_call(f, std::move(tuple));
}
}
else {
std::cout << "max size reached. `done` to finish: " << std::flush;

std::string input{};
std::cin >> input;

if(input == std::string{"done"}) {
return f(std::move(tuple));
}
else {
std::cout << "ERROR: invalid input" << std::endl;// `cout` or `cerr` here?
return tuplify_cin_and_call(f, std::move(tuple));
}
}
}

int main() {
tuplify_cin_and_call(
[] (auto tuple) { std::cout << __PRETTY_FUNCTION__ << std::endl; }
);
return 0;
}

输出(包括我的键盘输入):

./main
auto tuplify_cin_and_call(F, Tuple) [with F = main()::<lambda(auto:1)>; Tuple = std::tuple<>]
`int`|`char` to append or `done` to finish: int
auto tuplify_cin_and_call(F, Tuple) [with F = main()::<lambda(auto:1)>; Tuple = std::tuple<int>]
`int`|`char` to append or `done` to finish: char
auto tuplify_cin_and_call(F, Tuple) [with F = main()::<lambda(auto:1)>; Tuple = std::tuple<int, char>]
`int`|`char` to append or `done` to finish: asdf
ERROR: invalid input
auto tuplify_cin_and_call(F, Tuple) [with F = main()::<lambda(auto:1)>; Tuple = std::tuple<int, char>]
`int`|`char` to append or `done` to finish: char char int
auto tuplify_cin_and_call(F, Tuple) [with F = main()::<lambda(auto:1)>; Tuple = std::tuple<int, char, char>]
`int`|`char` to append or `done` to finish: auto tuplify_cin_and_call(F, Tuple) [with F = main()::<lambda(auto:1)>; Tuple = std::tuple<int, char, char, char>]
`int`|`char` to append or `done` to finish: auto tuplify_cin_and_call(F, Tuple) [with F = main()::<lambda(auto:1)>; Tuple = std::tuple<int, char, char, char, int>]
max size reached. `done` to finish: asdf
ERROR: invalid input
auto tuplify_cin_and_call(F, Tuple) [with F = main()::<lambda(auto:1)>; Tuple = std::tuple<int, char, char, char, int>]
max size reached. `done` to finish: done
main()::<lambda(auto:1)> [with auto:1 = std::tuple<int, char, char, char, int>]

关于c++模板将某种类型的成员添加到类中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47517682/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com