1

以下の例で、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;
}

ここまでやってみたけどわからなかった!!

ありがとうございました。

4

1 に答える 1

3

padFormat各引数で呼び出してから連結したいと思います。したがって、あなたは書く必要があります

return (padFormat<width>(first) + ... + padFormat<width>(rest));

(余分な括弧が必要です。fold-expression を有効にするには、括弧で囲む必要があります。)

コリルリンク

于 2019-02-06T22:54:43.743 に答える