uniform initialization with variadic templates

半城伤御伤魂 提交于 2021-02-15 06:05:15

问题


I have a POD ChParam and it's a parameter in the variadic template function set. I'd like to pass to function arguments(constructor parameters) in curly braces p.set({ Param::D, 1000.f }, { Param::p, 2000.f }). And thought that the constructor will be called implicitly and the ChParam objects will be created. But it's impossible, I should explicitly create an object a.set(ChParam{ Param::D, 1000.f }, ChParam{ Param::p, 2000.f });

is it possible somehow to use the variant p.set({ Param::D, 1000.f }, { Param::p, 2000.f })?

#include <iostream>
using namespace std;

using Float = float;

enum class Param : size_t
{
    D = 0,
    p
};
struct ChParam
{
    Param tag_;
    Float value_;
};
class PipeCalcParams
{
private:
    Float D_, p_;
public:
    PipeCalcParams() : D_(0), p_(0) {}
    PipeCalcParams& set_D(Float D) { D_ = D; return *this; }
    PipeCalcParams& set_p(Float p) { p_ = p; return *this; }


    template <typename... Args>
    PipeCalcParams& set(const ChParam& p, Args&&... args) {
        set(p);
        return set(args...);
    }

    PipeCalcParams& set(const ChParam& p)
    {
        switch (p.tag_)
        {
        case Param::D:
            set_D(p.value_);
            break;
        case Param::p:
            set_p(p.value_);
            break;
        }

        return *this;
    }

};

int main() {
    PipeCalcParams a;
    a.set(ChParam{ Param::D, 1000.f }, ChParam{ Param::p, 2000.f });//OK

    PipeCalcParams p;
    p.set({ Param::D, 1000.f }, { Param::p, 2000.f });//error: no matching function for call to 'PipeCalcParams::set(<brace-enclosed initializer list>, <brace-enclosed initializer list>)' p.set({ Param::D, 1000.f }, { Param::p, 2000.f });
    return 0;
}

回答1:


It is not directly possible to use

{ Param::D, 1000.f }

as a function parameter when it needs to be deduced. The reason for this is a braced initializer list has no type. Since it does not have a type, a type cannot be deduced by the compiler. You have to help it along. You can do what you did and specify the type like

ChParam{ Param:D, 1000.f }

Or you can specify the type of object you are expecting. If you want a variable numbers of the same types then a std::intializer_list will work. It allows the compiler to construct the elements from the individual braced initializer lists. Using that your code would look like

PipeCalcParams& set(std::initializer_list<ChParam> args)

And when you call it you would use

p.set({{ Param::D, 1000.f }, { Param::p, 2000.f }})

Do note the extra set of curly braces used. The outermost set declares the std::intializer_list and each inner set declares each ChParam in the list.



来源:https://stackoverflow.com/questions/39640553/uniform-initialization-with-variadic-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!