可变模板递归编译时间函数

variadic template recursive compiletime function

本文关键字:编译 时间 函数 递归      更新时间:2024-09-30

在可变模板中存在一些情况,我不明白它们为什么不起作用。例如,我有一个存储几个字符的模板类型:

template<char...chars> struct Test{
};
// If I try to print it with parameter packs everything okay.
template<char...chars> 
constexpr std::ostream& toOstream( std::ostream & os , Test <chars...> ) { 
return ( os << ... << chars ) ;
};

但我不明白为什么递归版本不起作用(这是simmilarhttps://stackoverflow.com/users/1365260/example的版本在如何编写可变模板递归函数?(:

// Case A
template<char c=0, char...chars> 
constexpr std::ostream& toOstream2( std::ostream & os , Test <c,chars...> ) { 
return sizeof...(chars) == 0 ? ( os << c ) :
toOstream2( os << c, Test<chars...>() ) ;
};
// Case B: Nor I understand why can't I use it inside another class:
template< class Base >
struct Deriv{
static constexpr std::ostream& toOstream( std::ostream & os ) { 
return ( os << Base() );
};
};
// main program:
int main(void){
toOstream( cout, Test<'k','a','b'>() ) << endl;
//toOstream2( cout, Test<'k','a','b'>() ) << endl;  // this gives error. Case A
//Deriv< Test<'k','a','b'> >().toOstream( cout ) << endl;  // this gives error. Case B
return 1;
}

程序的输出为:

kab

如果我取消对案例A的注释,则错误为:

xxxxxxxxxxx: In instantiation of ‘constexpr std::ostream& toOstream2(std::ostream&, Test<c, chars ...>) [with char c = 'b'; char ...chars = {}; std::ostream = std::basic_ostream<char>]’:
xxxxxxxxxxx:   recursively required from ‘constexpr std::ostream& toOstream2(std::ostream&, Test<c, chars ...>) [with char c = 'a'; char ...chars = {'b'}; std::ostream = std::basic_ostream<char>]’
...etc
xxxxxxxxxxxxxxxx: note:   candidate expects 2 arguments, 0 provided
|         toOstream2( os << c, Test<chars...>() ) ;
|         ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

对于案例B:

error: no match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘Test<'k', 'a', 'b'>’)
60 |         return ( os << Base() );
|                ~~~~~^~~~~~~~~~~

我正在使用g++10.2.1 20210110

有什么提示吗?

编辑:看了答案后,我想知道为什么这个编译没有问题,而我的案例A没有

template<int First=0, int... Rest>
constexpr int fauo()
{
return sizeof...(Rest)==0 ? First : First + fauo<Rest...>();
}
#include<iostream>
int main(void){
std::cout << fauo<0,9>() << std::endl;
return 0;
}

对于不适用于类的函数,有什么特别的东西吗?

要使其工作,您需要在编译时解析sizeof...(char)。如果:,则可以使用constexpr

template <char c, char... chars>
constexpr std::ostream& toOstream2(std::ostream& os, Test<c, chars...>) {
if constexpr (sizeof...(chars) == 0) return os << c;
else return toOstream2(os << c, Test<chars...>());
};

或者C++17之前的版本,使用重载:

template <char c>
constexpr std::ostream& toOstream2(std::ostream& os, Test<c>) {
return os << c;
};
template <char c, char... chars>
constexpr std::ostream& toOstream2(std::ostream& os, Test<c, chars...>) {
return toOstream2(os << c, Test<chars...>());
};

对于情况B,您需要使用Base实例调用toOstream2,而不是尝试执行os << Base(),因为您尚未为Base定义operator<<

template <class Base>
struct Deriv {
static constexpr std::ostream& toOstream(std::ostream& os) {
return toOstream2(os, Base{});
};
};

添加operator<<过载后,您的案例B可能如下所示:

#include <iostream>
template <char... chars> struct Test {};
template <char c, char... chars>
std::ostream& operator<<(std::ostream& os, Test<c, chars...>) {
if constexpr (sizeof...(chars) == 0) return os << c;
else return os << c << Test<chars...>{};
};
template <class Base>
struct Deriv {
friend std::ostream& operator<<(std::ostream& os, const Deriv<Base>&) {
return os << Base{};
};
};
int main() {
std::cout << Test<'k','a','b'>() << 'n';        // prints "kab"
std::cout << Deriv<Test<'k','a','b'>>() << 'n'; // prints "kab"
}

第一个(案例A(不起作用,因为您希望模板至少有一个参数。如果您指定了默认参数,情况也是如此。要使其工作,只需在toOstream2()定义为:之前允许一个0字符重载

constexpr std::ostream& toOstream2( std::ostream & os , Test <> ) { return os; }

第二个(案例B(不起作用,因为您还没有调用toOstream2(),但希望operator<<起作用。如果需要,您需要将函数命名为operator<<,而不是toOstream2()