提问人:glades 提问时间:5/28/2022 最后编辑:JeJoglades 更新时间:5/28/2022 访问量:98
基于传递给构造函数的参数数的推导
Deduction guide based on number of parameters passed to constructor
问:
这是我正在尝试的似乎不起作用的东西:我想根据类对象的实例化方式切换编译时开关。如果只有一个构造函数参数,则应等于 ,否则为 (我的实现具有更多构造函数,其中开关应默认为 .LengthOpt
false
true
true
我试图创建一个演绎指南,但显然,如果模板参数没有显示为构造函数参数(这是一个真正的失败者),它就不起作用。有谁知道这个问题不太冗长的解决方案?
代码:
#include <cstring>
#include <iostream>
const char* str = "some input string";
template <bool LengthOpt>
class some_class
{
public:
some_class(const char*) {
std::cout << std::boolalpha << LengthOpt << std::endl;
}
some_class(const char*, size_t len) {
std::cout << std::boolalpha << LengthOpt << std::endl;
}
};
template <bool LengthOpt> some_class(const char*) -> some_class<false>;
template <bool LengthOpt> some_class(const char*, size_t) -> some_class<true>;
int main()
{
some_class A(str);
some_class B(str, strlen(str));
}
错误:
<source>:19:27: error: deduction guide template contains a template parameter that cannot be deduced
template <bool LengthOpt> some_class(const char*) -> some_class<false>;
^
<source>:19:16: note: non-deducible template parameter 'LengthOpt'
template <bool LengthOpt> some_class(const char*) -> some_class<false>;
^
<source>:20:27: error: deduction guide template contains a template parameter that cannot be deduced
template <bool LengthOpt> some_class(const char*, size_t) -> some_class<true>;
^
<source>:20:16: note: non-deducible template parameter 'LengthOpt'
template <bool LengthOpt> some_class(const char*, size_t) -> some_class<true>;
^
<source>:24:16: error: no viable constructor or deduction guide for deduction of template arguments of 'some_class'
some_class A(str);
^
<source>:11:5: note: candidate template ignored: couldn't infer template argument 'LengthOpt'
some_class(const char*) {
^
<source>:19:27: note: candidate template ignored: couldn't infer template argument 'LengthOpt'
template <bool LengthOpt> some_class(const char*) -> some_class<false>;
^
<source>:8:7: note: candidate template ignored: could not match 'some_class<LengthOpt>' against 'const char *'
class some_class
^
<source>:14:5: note: candidate function template not viable: requires 2 arguments, but 1 was provided
some_class(const char*, size_t len) {
^
<source>:20:27: note: candidate function template not viable: requires 2 arguments, but 1 was provided
template <bool LengthOpt> some_class(const char*, size_t) -> some_class<true>;
^
<source>:25:16: error: no viable constructor or deduction guide for deduction of template arguments of 'some_class'
some_class B(str, strlen(str));
^
<source>:14:5: note: candidate template ignored: couldn't infer template argument 'LengthOpt'
some_class(const char*, size_t len) {
^
<source>:20:27: note: candidate template ignored: couldn't infer template argument 'LengthOpt'
template <bool LengthOpt> some_class(const char*, size_t) -> some_class<true>;
^
<source>:11:5: note: candidate function template not viable: requires 1 argument, but 2 were provided
some_class(const char*) {
^
<source>:8:7: note: candidate function template not viable: requires 1 argument, but 2 were provided
class some_class
^
<source>:19:27: note: candidate function template not viable: requires 1 argument, but 2 were provided
template <bool LengthOpt> some_class(const char*) -> some_class<false>;
^
4 errors generated.
ASM generation compiler returned: 1
<source>:19:27: error: deduction guide template contains a template parameter that cannot be deduced
template <bool LengthOpt> some_class(const char*) -> some_class<false>;
^
<source>:19:16: note: non-deducible template parameter 'LengthOpt'
template <bool LengthOpt> some_class(const char*) -> some_class<false>;
^
<source>:20:27: error: deduction guide template contains a template parameter that cannot be deduced
template <bool LengthOpt> some_class(const char*, size_t) -> some_class<true>;
^
<source>:20:16: note: non-deducible template parameter 'LengthOpt'
template <bool LengthOpt> some_class(const char*, size_t) -> some_class<true>;
^
<source>:24:16: error: no viable constructor or deduction guide for deduction of template arguments of 'some_class'
some_class A(str);
^
<source>:11:5: note: candidate template ignored: couldn't infer template argument 'LengthOpt'
some_class(const char*) {
^
<source>:19:27: note: candidate template ignored: couldn't infer template argument 'LengthOpt'
template <bool LengthOpt> some_class(const char*) -> some_class<false>;
^
<source>:8:7: note: candidate template ignored: could not match 'some_class<LengthOpt>' against 'const char *'
class some_class
^
<source>:14:5: note: candidate function template not viable: requires 2 arguments, but 1 was provided
some_class(const char*, size_t len) {
^
<source>:20:27: note: candidate function template not viable: requires 2 arguments, but 1 was provided
template <bool LengthOpt> some_class(const char*, size_t) -> some_class<true>;
^
<source>:25:16: error: no viable constructor or deduction guide for deduction of template arguments of 'some_class'
some_class B(str, strlen(str));
^
<source>:14:5: note: candidate template ignored: couldn't infer template argument 'LengthOpt'
some_class(const char*, size_t len) {
^
<source>:20:27: note: candidate template ignored: couldn't infer template argument 'LengthOpt'
template <bool LengthOpt> some_class(const char*, size_t) -> some_class<true>;
^
<source>:11:5: note: candidate function template not viable: requires 1 argument, but 2 were provided
some_class(const char*) {
^
<source>:8:7: note: candidate function template not viable: requires 1 argument, but 2 were provided
class some_class
^
<source>:19:27: note: candidate function template not viable: requires 1 argument, but 2 were provided
template <bool LengthOpt> some_class(const char*) -> some_class<false>;
^
4 errors generated.
Execution build compiler returned: 1
答:
5赞
HolyBlackCat
5/28/2022
#1
您不需要模板参数,只需执行以下操作:
some_class(const char *) -> some_class<false>;
some_class(const char *, size_t) -> some_class<true>;
推导指南类似于函数,但其所有模板参数都必须从其函数参数中推导出来。
template <bool LengthOpt> some_class(const char*) -> ...
变成类似 的东西,其中不可演绎。template <bool LengthOpt> void foo(const char*)
LengthOpt
评论