Giter VIP home page Giter VIP logo

Comments (2)

dtolnay avatar dtolnay commented on August 23, 2024 2

Thanks! I agree that an implicit construction from char const* to rust::String should be fine. I ended up changing only these two constructors:

explicit Box(const T &val);
explicit Box(T &&val);

which is consistent with there being no implicit constructor from T to unique_ptr<T>.

from cxx.

bruxisma avatar bruxisma commented on August 23, 2024

in C++20, we have the option of making constructors conditionally explicit. This could be one path to take.explicit means that doing something like

rust::String some_function () {
  return { "hello" };
}

Will not compile. If you want these to be 'free-form', then it might stand to make them implicit.

That said, C++17's std::string has added a constructor which is explicit and is for converting between any T provided the following:

Implicitly converts t to a string view sv as if by std::basic_string_view<CharT, Traits> sv = t;, then initializes the string with the contents of sv, as if by basic_string(sv.data(), sv.size(), alloc). This overload only participates in overload resolution if std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> is true and std::is_convertible_v<const T&, const CharT*> is false.

I will say that while some C++ committee folks are against implicit constructors, I tend to permit them for 'basic' types, even from other languages. (after all, if I return "hello" from a C++ function that is being called by Python, it would stand to reason that I want it to be a python string, not a C char const*).

That said, however, std::unique_ptr has an explicit constructor for taking a single pointer, but make_unique, and make_unique_for_overwrite are provided to 'get around' this option. (However, this is because make_unique is the default 'use new T(std::forward<Args>(args)...)', while unique_ptr(pointer) is provided to allow you to allocate from wherever. Since allocators aren't stabilized in Rust yet, it's not really an issue on that side of things, but it's something to keep in mind, I suppose. 🙂

from cxx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.