小编stg*_*pns的帖子

使用包含字符串的模板对象创建可变参数模板

我想创建一个模板,它将采用包含字符串的特定类的变量号。当我尝试创建这样的模板类时,它抛出 没有与参数列表匹配的构造函数“A”的实例

A类:

template<template_string s>
class A {};
Run Code Online (Sandbox Code Playgroud)

B类:

template<A... As>
class B {};
Run Code Online (Sandbox Code Playgroud)

模板_字符串.h:

template<size_t _N>
struct template_string {
char c_str[_N];

    constexpr template_string(const char(&text)[_N]) {
        std::copy(text, text+_N, c_str);
    }

    constexpr std::string std_str() const noexcept {
        return std::string(c_str);
    }
    constexpr std::string_view str_view() const noexcept {
        return std::string_view(c_str);
    }

    constexpr size_t length() const noexcept {
        return _N-1;
    }

};
Run Code Online (Sandbox Code Playgroud)

我想通过模板和类明确地做到这一点。我需要做什么才能构建 B 类:

B<A<"text">(), A<"other text">(),....> b;
Run Code Online (Sandbox Code Playgroud)

c++ templates variadic-templates c++20

1
推荐指数
1
解决办法
79
查看次数

标签 统计

c++ ×1

c++20 ×1

templates ×1

variadic-templates ×1