enable_if and conversion operator?

后端 未结 4 2034
天命终不由人
天命终不由人 2020-12-30 04:18

Any chance to use enable_if with a type conversion operator? Seems tricky, since both return type and parameters list are implicit.

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-30 05:17

    While I can understand the theoritecal interest in the question, I personally refrain from using conversion operators as much as possible.

    The only one I ever use with consistence is the conversion to a pseudo-boolean (using the Safe Bool idiom), for smart-pointers or proxies, and as noted I use a trick to actually prevent the full boolean semantic...

    If I ever want to facilitate conversions, I much prefer something along the line of:

    template 
    T to() const;
    

    which does not suffer from the limitations (in term of signature) of the conversion operator and requires explicit invocation, just because it's a bit clearer.

提交回复
热议问题