基于传递给构造函数的参数数量的推导指南
Deduction guide based on number of parameters passed to constructor
这是我正在尝试但似乎不起作用的方法:我想根据 class 对象的实例化方式来切换编译时开关。如果只有一个构造函数参数,LengthOpt
应等于 false
,否则等于 true
(我的实现有更多构造函数,其中开关应默认为 true
.
我试图创建一个推导指南,但如果模板参数没有显示为构造函数参数(这是一个真正的失败者),显然它不起作用。有谁知道这个问题的不太详细的解决方案吗?
Code:
#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
您不需要模板参数,只需执行以下操作:
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
是 non-deducible.
这是我正在尝试但似乎不起作用的方法:我想根据 class 对象的实例化方式来切换编译时开关。如果只有一个构造函数参数,LengthOpt
应等于 false
,否则等于 true
(我的实现有更多构造函数,其中开关应默认为 true
.
我试图创建一个推导指南,但如果模板参数没有显示为构造函数参数(这是一个真正的失败者),显然它不起作用。有谁知道这个问题的不太详细的解决方案吗?
Code:
#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
您不需要模板参数,只需执行以下操作:
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
是 non-deducible.