另一个方向的隐含转换

Tho*_*mas 1 c++

如果无法修改包装类,则包装类很好.使用包装器,我可以添加某些功能和方便,我仍然可以通过使用从包装器到包装类型的隐式转换,使用包装类型的函数接受包装器.像这样的东西:

struct vec {
    __m128 m128;
    inline operator __m128 &() {
        return m128;
    }
    //convenience to add functionality related to the wrapped variable inserted here
}
Run Code Online (Sandbox Code Playgroud)

这很好用.

现在我的问题是,当你无法访问包装类型的源或无法修改它时,你可以隐式地转换从__m128到vec的另一种方式吗?

Tar*_*ama 5

是的,只需编写一个__m128没有标记的构造函数explicit:

vec (__m128 m128) //maybe take by const-ref (I don't know what __m128 is)
  : m128(m128)
{}
Run Code Online (Sandbox Code Playgroud)