Are explicit conversion operators allowed in braced initializer lists?

后端 未结 3 487
逝去的感伤
逝去的感伤 2021-01-11 09:28

The following code compiles with GCC 4.9.2 but not with Clang 3.5.0:

#include 

class Foo
{
public:
  explicit operator std::string() const;
};         


        
3条回答
  •  旧巷少年郎
    2021-01-11 09:51

    From [class.conv.fct]/2:

    A conversion function may be explicit (7.1.2), in which case it is only considered as a user-defined conversion for direct-initialization (8.5).

    So the question is how you initialize your objects. Clearly baz is direct-initialized, so this works. By contrast, bar is direct-list-initialized, but not direct-initialized, and so the explicit conversion is not available.

提交回复
热议问题