C++ trick I pulled today. Like an explicit constructor but context dependent. Any alternatives from folks who’ve needed to do similar? One thing I still need to dig into a little deeper is how copy elision behaves here.

  • @golvok
    link
    17 months ago

    I’m wondering where you wanted to use this. Was it a generic context, where you didn’t know what types you were getting? Also, I think it will interfere with the retern-value optimization.

    • @[email protected]OP
      link
      fedilink
      2
      edit-2
      7 months ago

      Yes, that’s right, generic context, and you may be right on return value optimization. It was for implementing a collection of numerical functions that take array arguments, where the elements of those arrays could be of various arithmetic types, and the return type should be an array of a particular arithmetic type given promotion etc. The implementation was generic, and I was wanting to validate its correctness wrt return values having the correct arithmetic type without implicit copy.

      • @[email protected]OP
        link
        fedilink
        27 months ago

        For the array type it can be useful to allow implicit copy to different arithmetic types (design choice, I’m now back to explicit constructors to disallow this for what it’s worth). If allowed though, I still wanted a compile time check like this to ensure that it wasn’t happening by accident in particular circumstances.