Skip to main content
address other comments from the OP
Source Link
utnapistim
  • 5.3k
  • 18
  • 25

Is there a standard way to indicate that a function returns a new pointer?

No, there is no "standard way" (but there is aan API design policy currently considered "best practice").

Because of this ambiguity ("what does a function returning a pointer want me to do with it?"), it is currently considered best practice to impose the lifetime and ownership policy, through the return type:

<vertex pointer type> new_vertex(const Options& options); 

<vertex pointer type> can be std::unique_ptr ("new_vertex doesn't own the pointer"), or std::shared_ptr ("client code doesn't own the pointer"), or something else, that has clearly defined ownership semantics (for example, Vertex const * const would indicate to client code "read the address and values, but change neither/don't delete the pointer").

Generally, you should not return a raw pointer (but in some cases, "practicality beats purity").

TLDR: there is a best practice (yes), but not a standard way (in the language).

Edit:

where the function is only intended to be used to within a class that owns the Vertex

If the class owns the Vertex, I would write it like this:

class SomeClass // owns the vertex { void do_stuff() // uses the vertex internally { init_vertex(); // see below assert(vertex.get()); // use vertex as needed } private: // "class owns the Vertex" std::unique_ptr<Vertex> vertex; // sets the internal vertex // function doesn't return a pointer of any kind void init_vertex(const Options& options); // or "reset_", "refresh_", // or "make_" vertex, // if the pointer can change // throughout the lifetime // of a SomeClass instance }; 

Is there a standard way to indicate that a function returns a new pointer?

No, there is no "standard way" (but there is a policy currently considered "best practice").

Because of this ambiguity ("what does a function returning a pointer want me to do with it?"), it is currently considered best practice to impose the lifetime and ownership policy, through the return type:

<vertex pointer type> new_vertex(const Options& options); 

<vertex pointer type> can be std::unique_ptr ("new_vertex doesn't own the pointer"), or std::shared_ptr ("client code doesn't own the pointer"), or something else, that has clearly defined ownership semantics (for example, Vertex const * const would indicate to client code "read the address and values, but change neither/don't delete the pointer").

Generally, you should not return a raw pointer (but in some cases, "practicality beats purity").

TLDR: there is a best practice (yes), but not a standard way (in the language).

Is there a standard way to indicate that a function returns a new pointer?

No, there is no "standard way" (but there is an API design policy currently considered "best practice").

Because of this ambiguity ("what does a function returning a pointer want me to do with it?"), it is currently considered best practice to impose the lifetime and ownership policy, through the return type:

<vertex pointer type> new_vertex(const Options& options); 

<vertex pointer type> can be std::unique_ptr ("new_vertex doesn't own the pointer"), or std::shared_ptr ("client code doesn't own the pointer"), or something else, that has clearly defined ownership semantics (for example, Vertex const * const would indicate to client code "read the address and values, but change neither/don't delete the pointer").

Generally, you should not return a raw pointer (but in some cases, "practicality beats purity").

TLDR: there is a best practice (yes), but not a standard way (in the language).

Edit:

where the function is only intended to be used to within a class that owns the Vertex

If the class owns the Vertex, I would write it like this:

class SomeClass // owns the vertex { void do_stuff() // uses the vertex internally { init_vertex(); // see below assert(vertex.get()); // use vertex as needed } private: // "class owns the Vertex" std::unique_ptr<Vertex> vertex; // sets the internal vertex // function doesn't return a pointer of any kind void init_vertex(const Options& options); // or "reset_", "refresh_", // or "make_" vertex, // if the pointer can change // throughout the lifetime // of a SomeClass instance }; 
Source Link
utnapistim
  • 5.3k
  • 18
  • 25

Is there a standard way to indicate that a function returns a new pointer?

No, there is no "standard way" (but there is a policy currently considered "best practice").

Because of this ambiguity ("what does a function returning a pointer want me to do with it?"), it is currently considered best practice to impose the lifetime and ownership policy, through the return type:

<vertex pointer type> new_vertex(const Options& options); 

<vertex pointer type> can be std::unique_ptr ("new_vertex doesn't own the pointer"), or std::shared_ptr ("client code doesn't own the pointer"), or something else, that has clearly defined ownership semantics (for example, Vertex const * const would indicate to client code "read the address and values, but change neither/don't delete the pointer").

Generally, you should not return a raw pointer (but in some cases, "practicality beats purity").

TLDR: there is a best practice (yes), but not a standard way (in the language).