我可以在此示例中使用折叠表达式吗

时间:2019-02-06 22:51:10

标签: c++ templates c++17 fold-expression

我想知道在下面的示例中是否可以使用fold表达式(以及如何编写)。

#include <iostream>
#include <type_traits>
#include <typeinfo>
#include <sstream>
#include <iomanip>

template<int width>
std::string padFormat()
{
    return "";
}

template<int width, typename T>
std::string padFormat(const T& t)
{
    std::ostringstream oss;
    oss << std::setw(width) << t;
    return oss.str();
}

template<int width, typename T, typename ... Types>
std::string padFormat(const T& first, Types ... rest)
{
    return (padFormat<width>(first + ... + rest)); //Fold expr here !!!
}

int main()
{
    std::cout << padFormat<8>("one", 2, 3.0) << std::endl;
    std::cout << padFormat<4>('a', "BBB", 9u, -8) << std::endl;
    return 0;
}

我到目前为止已经尝试了,但是我没有弄清楚!!

谢谢。

1 个答案:

答案 0 :(得分:3)

我猜您想在每个参数上调用 export const getEventListeners = event => state => { // return state object that derives from an event }; ... this.setState(getEventListeners(event)); 然后连接。因此,您必须写

padFormat

(需要额外的括号;必须在括号中包含倍数表达式才能有效。)

Coliru link