Is the [Ref] attribute of a const record parameter useful?


username

With the latest Delphi version (Berlin/10.1/24), is the [Ref] attribute really necessary?

I'm asking this because the online documentation says:

Constant parameters can be passed to functions by value or by reference, depending on the specific compiler used . To force the compiler to pass constant parameters by reference, you can use the [Ref] decorator with the const keyword.

David Heffernan

Pretty much as described in the documentation. You would use [ref]if you have a reason to force passing parameters by reference. One example I can think of is interop. Suppose you are calling an API function defined as follows:

typedef struct {
    int foo;
} INFO;

int DoStuff(const INFO *lpInfo);

In Pascal, you might want to import it like this:

type
  TInfo = record
    foo: Integer;
  end;

function DoStuff(const Info: TInfo): Integer; cdecl; external libname;

But being TInfoso small, the compiler might choose to pass the structure by value. So you can annotate [ref]to force the compiler to pass the parameter as a reference.

function DoStuff(const [ref] Info: TInfo): Integer; cdecl; external libname;

Related


Is the [Ref] attribute of a const record parameter useful?

username With the latest Delphi version (Berlin/10.1/24), is the [Ref] attribute really necessary? I'm asking this because the online documentation says: Constant parameters can be passed to functions by value or by reference, depending on the specific compile

Is the [Ref] attribute of a const record parameter useful?

username With the latest Delphi version (Berlin/10.1/24), is the [Ref] attribute really necessary? I'm asking this because the online documentation says: Constant parameters can be passed to functions by value or by reference, depending on the specific compile

Is the [Ref] attribute of a const record parameter useful?

username With the latest Delphi version (Berlin/10.1/24), is the [Ref] attribute really necessary? I'm asking this because the online documentation says: Constant parameters can be passed to functions by value or by reference, depending on the specific compile

Is the [Ref] attribute of a const record parameter useful?

username With the latest Delphi version (Berlin/10.1/24), is the [Ref] attribute really necessary? I'm asking this because the online documentation says: Constant parameters can be passed to functions by value or by reference, depending on the specific compile

Pass literal as const ref parameter

innocent bystander Imagine the following simplified code: #include <iostream> void foo(const int& x) { do_something_with(x); } int main() { foo(42); return 0; } (1) Besides optimization, what happens when 42 is passed to foo? Does the compiler stick 42 somew

Pass literal as const ref parameter

innocent bystander Imagine the following simplified code: #include <iostream> void foo(const int& x) { do_something_with(x); } int main() { foo(42); return 0; } (1) Besides optimization, what happens when 42 is passed to foo? Does the compiler stick 42 somew

Pass literal as const ref parameter

innocent bystander Imagine the following simplified code: #include <iostream> void foo(const int& x) { do_something_with(x); } int main() { foo(42); return 0; } (1) Besides optimization, what happens when 42 is passed to foo? Does the compiler stick 42 somew

Pass literal as const ref parameter

innocent bystander Imagine the following simplified code: #include <iostream> void foo(const int& x) { do_something_with(x); } int main() { foo(42); return 0; } (1) Besides optimization, what happens when 42 is passed to foo? Does the compiler stick 42 somew

Pass literal as const ref parameter

innocent bystander Imagine the following simplified code: #include <iostream> void foo(const int& x) { do_something_with(x); } int main() { foo(42); return 0; } (1) Besides optimization, what happens when 42 is passed to foo? Does the compiler stick 42 somew

function template specialization with const ref parameter

use The following code compiles fine . #include <iostream> struct rgb8{ uint8_t r() const {return 0;}; }; template<typename L, typename P> L pixelToLevel(P p) { return static_cast<L>(p); } template<> uint8_t pixelToLevel<uint8_t, rgb8>(rgb8 p) { /

function template specialization with const ref parameter

use The following code compiles fine . #include <iostream> struct rgb8{ uint8_t r() const {return 0;}; }; template<typename L, typename P> L pixelToLevel(P p) { return static_cast<L>(p); } template<> uint8_t pixelToLevel<uint8_t, rgb8>(rgb8 p) { /

function template specialization with const ref parameter

use The following code compiles fine . #include <iostream> struct rgb8{ uint8_t r() const {return 0;}; }; template<typename L, typename P> L pixelToLevel(P p) { return static_cast<L>(p); } template<> uint8_t pixelToLevel<uint8_t, rgb8>(rgb8 p) { /

function template specialization with const ref parameter

use The following code compiles fine . #include <iostream> struct rgb8{ uint8_t r() const {return 0;}; }; template<typename L, typename P> L pixelToLevel(P p) { return static_cast<L>(p); } template<> uint8_t pixelToLevel<uint8_t, rgb8>(rgb8 p) { /

Is it useful to move const objects?

virtual software company I realize that the common sense "can't move const objects" isn't entirely true. If you declare the move ctor as, you can X(const X&&); The complete example is as follows: #include <iostream> struct X { X() = default; X(const

Is it useful to move const objects?

virtual software company I realize that the common sense "can't move const objects" isn't entirely true. If you declare the move ctor as, you can X(const X&&); The complete example is as follows: #include <iostream> struct X { X() = default; X(const

Is it useful to move const objects?

virtual software company I realize that the common sense "can't move const objects" isn't entirely true. If you declare the move ctor as, you can X(const X&&); The complete example is as follows: #include <iostream> struct X { X() = default; X(const

Is it useful to move const objects?

virtual software company I realize that the common sense "can't move const objects" isn't entirely true. If you declare the move ctor as, you can X(const X&&); The complete example is as follows: #include <iostream> struct X { X() = default; X(const

Is it useful to move const objects?

virtual software company I realize that the common sense "can't move const objects" isn't entirely true. If you declare the move ctor as, you can X(const X&&); The complete example is as follows: #include <iostream> struct X { X() = default; X(const

Is it useful to move const objects?

virtual software company I realize that the common sense "can't move const objects" isn't entirely true. If you declare the move ctor as, you can X(const X&&); The complete example is as follows: #include <iostream> struct X { X() = default; X(const

What is std::ref useful in this function?

Martin Why should calling std::ref be preferred over not calling it at all? template<class F, class...Ts> F for_each_arg(F f, Ts&&...a) { return (void)initializer_list<int>{(ref(f)((Ts&&)a), 0)...}, f; // why not return (void)initializer_list<int>{(f((Ts&&

What is the useful std::ref in this function?

Martin Why should calling std::ref be preferred over not calling it at all? template<class F, class...Ts> F for_each_arg(F f, Ts&&...a) { return (void)initializer_list<int>{(ref(f)((Ts&&)a), 0)...}, f; // why not return (void)initializer_list<int>{(f((Ts&&

What is std::ref useful in this function?

Martin Why should calling std::ref be preferred over not calling it at all? template<class F, class...Ts> F for_each_arg(F f, Ts&&...a) { return (void)initializer_list<int>{(ref(f)((Ts&&)a), 0)...}, f; // why not return (void)initializer_list<int>{(f((Ts&&

What is std::ref useful in this function?

Martin Why should calling std::ref be preferred over not calling it at all? template<class F, class...Ts> F for_each_arg(F f, Ts&&...a) { return (void)initializer_list<int>{(ref(f)((Ts&&)a), 0)...}, f; // why not return (void)initializer_list<int>{(f((Ts&&

Are python attribute getters useful?

squid I wonder if there is any use of explicitly using getter decorators for class properties: class A: @property def p(self): return self._p @p.setter def p(self, val): assert p < 1000 self._p = val @p.getter def

Are python attribute getters useful?

squid I wonder if there is any use of explicitly using getter decorators for class properties: class A: @property def p(self): return self._p @p.setter def p(self, val): assert p < 1000 self._p = val @p.getter def

Are python attribute getters useful?

squid I wonder if there is any use of explicitly using getter decorators for class properties: class A: @property def p(self): return self._p @p.setter def p(self, val): assert p < 1000 self._p = val @p.getter def

Is it useful to declare a reference type as const?

Naval Why would anyone do this: const object useless = null; const IEnumerable meaningless = null; Eric Lippert said that functionality is not implemented by default, and every possibility adds effort in testing, maintenance, etc. Why use the null value of a

Is it useful to declare a reference type as const?

Naval Why would anyone do this: const object useless = null; const IEnumerable meaningless = null; Eric Lippert said that functionality is not implemented by default, and every possibility adds effort in testing, maintenance, etc. Why use the null value of a