将模板与类模板扣除占位符参数匹配
Match template with Class Template Deduction Placeholder param
尝试在GCC 10.1中使用字符串文字作为非类型模板参数。具有以下代码:
#include <cstddef>
#include <algorithm>
#include <iostream>
template<std::size_t n> struct fixed_string {
constexpr fixed_string(char const (&s)[n]) {
std::copy_n(s, n, this->el);
}
constexpr fixed_string(fixed_string<n> const& s) {
std::copy_n(s.el, n, this->el);
}
constexpr bool operator==(fixed_string const&) const = default;
constexpr auto operator<=>(fixed_string const&) const = default;
char el[n];
};
template<std::size_t n> fixed_string(char const(&)[n])->fixed_string<n>;
template<fixed_string str>
class Base {
public:
Base() {
std::cout << str.el << std::endl;
}
};
template<fixed_string str>
class Derived : public Base<str> {
};
int main(void) {
Derived<"Hello World"> x;
}
Base
本身工作得很好,它试图弄清楚如何将字符串文本传递到类层次结构中。我以为复制构造函数会起作用,GCC抛出了这个可爱的错误消息,并嘲笑我的尝试:
error: no matching function for call to ‘fixed_string(fixed_string<...auto...>)’
note: candidate: ‘template<long unsigned int n> fixed_string(const fixed_string<n>&)-> fixed_string<n>’
constexpr fixed_string(fixed_string<n> const& s) {
^~~~~~~~~~~~
note: template argument deduction/substitution failed:
note: mismatched types ‘const fixed_string<n>’ and ‘fixed_string<...auto...>’
酷。所以CCD_ 2是GCC;类模板扣除占位符";我不知道如何威胁编译器将其与我的函数调用相匹配。有人知道如何在这里胁迫GCC吗?或者,如何通过类层次结构传播字符串文字?
有趣的是,将str.el
传递给Base
模板会导致GCC崩溃。
这肯定是一个gcc错误。我经历过,并找到了这个解决方法:
template<std::size_t n> struct fixed_string {
consteval fixed_string(char const (&s)[n]) {
std::copy_n(s, n, this->el);
}
consteval fixed_string(const fixed_string<n>& s) = default;
consteval bool operator==(fixed_string const&) const = default;
consteval auto operator<=>(fixed_string const&) const = default;
char el[n];
static const std::size_t size = n;
};
template<std::size_t n> fixed_string(char const(&)[n])->fixed_string<n>;
template<std::size_t n, fixed_string<n> str>
class BaseImpl {
public:
BaseImpl() {
std::cout << str.el << std::endl;
}
};
template <fixed_string s>
using Base = BaseImpl<s.size, s>;
template<std::size_t n, fixed_string<n> str>
class DerivedImpl : public BaseImpl<n, str> {
};
template <fixed_string s>
using Derived = DerivedImpl<s.size, s>;
int main(void) {
Derived<"Hello World"> x;
}
或者
template<fixed_string str>
class Base {
public:
Base() {
std::cout << str.el << std::endl;
}
};
template <std::size_t n, fixed_string<n> s>
using BaseWrapper = Base<s>;
template<fixed_string str>
class Derived : public BaseWrapper<str.size, str> {
};
一般的想法是避免使用推导的参数来推导另一个参数。
相关文章:
- 将模板与类模板扣除占位符参数匹配
- C++初始化 std::function 时如何将占位符绑定到引用/引用参数?
- Tensorflow C++ 占位符初始化
- std::bind 不会接受 bind 占位符的 std::cref - 为什么?
- qt 中的占位符
- 我可以在 c++ 中使用占位符作为数组的索引吗?
- 为什么协程不允许使用占位符返回类型?
- TensorFlow类占位符以及其他定义输入张量的方法
- 非类型模板参数中的占位符类型是否涉及作为模板参数传递的函数的重载解析?
- 是否可以使用自动占位符来推断非类型模板参数中的函数结果
- std::绑定可变参数模板、绑定参数和占位符
- 将具有任意参数和占位符的函数存储在类中,然后再调用它
- 在另一个函数中使用std::bind返回对象和占位符作为参数
- 非类型模板参数的占位符类型在模板模板参数的情况下是否可以互换
- MPL占位符在默认模板参数中替换
- std::函数回调,参数采用观察者模式(寄存器主题上的占位符)
- 如何在c++中为不需要的参数使用占位符
- Boost绑定占位符参数等于可变模板参数的数量
- 为什么 std::bind 的占位符不使用非类型模板参数实现?
- c++,将自定义占位符与函数参数匹配