C++幼女先輩

プログラミング成分多め

クラスのコンストラクタを可変長テンプレートにして ごにょごにょ

前回なやんだ続きではあります

関数テンプレートは暗黙的インスタンス化可能だが、クラステンプレートは不可能 ならば コンストラクタを可変長テンプレートすればいいじゃない

[Wandbox]三へ( へ՞ਊ ՞)へ ハッハッ

#include<iostream>
#include<sstream>

using namespace std;

struct Hoge{
void stats_(){};
stringstream ss_;

template <class Head, class... Tail>
void stats_(Head&& head, Tail&&... tail){
    ss_ << head;  
    stats_(std::forward<Tail>(tail)...);
}

template <class... T>
    Hoge (T&&... t){
        stats_(std::forward<T>(t)...);
}

friend ostream& operator << (ostream& os, const Hoge& hoge);
    
};
ostream& operator << (ostream& os, const Hoge& hoge){
    os << hoge.ss_.str();
    return os;
}


int main()
{
    cout << Hoge("aaa,", "bbb,", "ccc,") << endl;
}

つまり 関数オブジェクトは

#include<iostream>
#include<sstream>

using namespace std;

struct Hoge{
void stats_(){};
stringstream ss_;

template <class Head, class... Tail>
void stats_(Head&& head, Tail&&... tail){
//    std::cout << head;
    ss_ << head;  
    stats_(std::forward<Tail>(tail)...);
}

template <class... T>
    Hoge& operator() (T&&... t){
        stats_(std::forward<T>(t)...);
    return *this;
}

friend ostream& operator << (ostream& os, const Hoge& hoge);
    
};
ostream& operator << (ostream& os, const Hoge& hoge){
    os << hoge.ss_.str();
    return os;
}


int main()
{
    cout << Hoge()("aaa,", "bbb,", "ccc,") << endl;
}

こうやればできたんだなーー

関数オブジェクトでは 自分を返して汎用的にしてみたけど、何を返すのが正しいのかは わからない