sách gpt4 ai đã đi

Tìm các tùy chọn/đối số/tham số mẫu không được gắn thẻ theo vị trí

In lại 作者:太空宇宙 更新时间:2023-11-04 11:29:12 35 4
mua khóa gpt4 Nike

Nói tóm lại:我想从可变参数模板参数中提取各种选项,但不仅通过标签而且通过那些参数的索引,这些参数是未知的 标签。我喜欢 boost 中的方法(例如 heap hoặc lockfree 策略),但想让它与 STL containers 兼容- 分配器参数。

Lời nói đầu

我目前正在为具有此签名的可变大小记录/对象的队列/缓冲区编写模板:

// current:
template ,
class... Opts> class rqueue;
// what I want:
template class rqueue;

我几乎没有其他可能的选择,我是这样描述的:

namespace tag {
struct allocator {}; ///< specify allocator for data storage
struct virtual_offset {}; ///< manage offset in virtual memory
struct fill_empty {}; ///< fill empty space (security or reconstruction)
struct reconstructible {}; ///< fill empty and leave one slot between wp&rp
} namespace opt {
/// allocator for data storage
template struct allocator: tag::allocator {
typedef Alloc type; };
/// type for offset in virtual memory
template struct virtual_offset: tag::virtual_offset {
typedef Off type; };
/// type and value to fill empty space
template struct fill_empty: tag::fill_empty {
typedef T type; static constexpr T value = V; };
/// make state pointers reconstructible by leaving one slot between wp&rp
template struct reconstructible
: tag::reconstructible, fill_empty {};
}

cách sử dụng

// basic queue for custom record class
rqueue;
// advanced record storage that can be written to a file and reconstructed back
rqueue<>, opt::reconstructible>;
// specialization for strings with custom allocator
rqueue;
// alternative to above
rqueue>;

选项包助手

namespace opt {
template struct bind {
template static constexpr bool has() {
return false; }
template
using get = Default; };
template struct bind {
riêng tư:
template static constexpr bool first() {
return std::is_same::value
|| std::is_base_of::value; }
template struct get_ {
typedef typename bind::template get type; };
template struct get_ {
typedef First type; };
công cộng:
template static constexpr bool has() {
return first() || bind::template has(); }
template
using get = typename get_()>::type; };
}

它没有The Boost Parameter Library那么先进,但完成了工作……到目前为止,包含未标记的必需参数和标记的可选参数列表。

Mã kiểm tra

cout << boolalpha;

typedef opt::bind<
opt::virtual_offset,
opt::reconstructible
> opts;
cout << opts::has() << endl;
cout << opts::has() << endl;
cout << opts::has() << endl;
cout << typeid(opts::get::type).name() << endl;
cout << typeid(opts::get::type).name() << endl;
cout << (int)opts::get::value << endl;

typedef opt::bind<> no;
cout << no::has() << endl;
cout << no::has() << endl;
cout << no::has() << endl;
cout << typeid(no::get).name() << endl;

typedef opt::bind<>> one;
cout << one::has() << endl;
cout << one::has() << endl;
cout << one::has() << endl;
cout << typeid(one::get).name() << endl;

câu hỏi

我正在搜索复杂的 boost 参数/元编程库并以 metafunctions 结尾它可以完成与我的小 helper 相同的工作(当然还有更多),但没有找到按索引提取未标记选项的解决方案。

  1. 我错过了吗?它在某处吗?
  2. 你能给我一些不同的解决方案吗?

也许我应该忘记它并坚持使用那些标签,或者编写一些复杂的帮助程序来过滤掉所有标记的选项并访问索引留下的选项......但在我投降或去之前很长的路要走,这里是问的好地方:)

Để ý:如果你引用了一些库,请留下如何使用它的描述。谢谢。

1 Câu trả lời

我已经设法让它工作了

(已提供的选项包助手的特化,与原始代码合并)

template struct tags {
template static constexpr bool match() {
return false; }};
template struct tags {
template static constexpr bool match() {
return std::is_same::value
|| std::is_base_of::value
|| tags::template match

cách sử dụng

template  class rqueue {
// bind tags and options
typedef opt::bind<><>
tag::virtual_offset, tag::fill_empty,
tag::reconstructible, tag::fixed_size>,
Opts...> opts;
công cộng:
// get first untagged option or byte if none
typedef typename opts::template at<0,byte>
value_type, record_type, *pointer, &reference;
// get second untagged option, or allocator option or std::allocator
typedef typename std::conditional<
(opts::count > 1),
typename opts::template at<1>,
typename opts::template get<>
std::allocator>>::type allocator_type;
//...shorter version of the above
typedef typename opts::template at<1,
typename opts::template get<>
std::allocator>> allocator_type_v2;
// get type specified as virtual_offset or void
typedef typename opts::template get<>
std::enable_if>::type offset_type;

关于c++ - 按位置查找未标记的模板选项/参数/参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25474036/

35 4 0
太空宇宙
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com