C++语法将ColorA拆分为浮点

C++ Syntax Split ColorA into floats?

本文关键字:拆分 ColorA 语法 C++      更新时间:2023-10-16

我记得我能做这样的事情,但我不记得是怎么做的。我想从ColorA数据类型中提取浮点,在C++中最短的语法是什么?

ColorA(r,g,b,a)=material.getAmbient();

如果您愿意提供to_tuple函数,可以在此处使用std::tie。元组转换函数有一个更糟糕的解决方法,所以这个解决方案是直接的。

struct ColorA
{
    float r, g, b, a;
    auto to_tuple() const
    {
        return std::make_tuple(r, g, b, a);
    }
};
int main()
{
    float r, g, b, a;
    ColorA color;
    std::tie(r, g, b, a) = color.to_tuple();
    return 0;
}