const T &arg vs. T arg
Use const T & arg
if sizeof(T)>sizeof(void*)
and use T arg
if sizeof(T) <= sizeof(void*)
Need to understand when parameters passed as (className& arg) in c++
The &
in void print (const MyClass& arg)
that arg
is passed by reference. It is C++s way to make pointers and things a little bit easier.
References allow you to manipulate a variable inside of a function and make the results visible on the outside too. So a bit like pointers. But you don't need to explicitly get the address of the variable to do that.
The const
statement is a way to prevent the described behavior. const
forbids the manipulation of arg
inside print
.
int vs const int&, And when I use it
You should generally use references if you plan on continuing to use the value you are passing into a function after that function completes. As you have highlighted, sometimes it is more costly to pass a reference. This can happen if your data is very small (you normally wouldn't bother passing integers as arguments), or if you have an opportunity to move
the contents of the variable (though this can be a more complicated topic).
Using references allows you to avoid copying data while still guaranteeing that the argument is valid, since unlike with pointers there is no null reference. References give you the opportunity of allowing the function to mutate the contents of your variable, or with const references you can prevent a function from modifying the variable you pass in. In general you should prefer to use references over pointers when you don't need features only available with a pointer, and prefer const references over references when you don't need mutation.
C++: const reference, before vs after type-specifier
No difference as const is read right-to-left with respect to the &, so both represent a reference to an immutable Fred instance.
Fred& const
would mean the reference itself is immutable, which is redundant; when dealing with const pointers both Fred const*
and Fred* const
are valid but different.
It's a matter of style, but I prefer using const
as a suffix since it can be applied consistently including const member functions.
C++: Const correctness and pointer arguments
You have it backwards:
const int * intPtr1; // Declares a pointer whose contents cannot be changed.
int * const intPtr2; // Declares a pointer that cannot be changed.
The following const
is indeed unnecessary, and there's no reason to put it in a function declaration:
void someFunc1(int * const arg);
However, you might want to put it in the function implementation, for the same reason that you might want to declare a local variable (or anything else) const
- the implementation may be easier to follow when you know that certain things won't change. You can do that whether or not it's declared const
in any other declarations of the function.
Define C++ conversion operator only if template arg is not const
A possible solution is use SFINAE to enable the operator only when T
is different from T const
.
For example (caution: code not tested)
template <typename U = T,
typename std::enable_if<false == std::is_same<U, T const>::value, int>::type = 0>
operator CContainer<U const>() const
{ /* here goes the code */ }
or, as suggested by Remy Lebeau (thanks) you can use std::is_const
template <typename U = T,
typename std::enable_if<false == std::is_const<U>::value, int>::type = 0>
operator CContainer<U const>() const
{ /* here goes the code */ }
Typescript type T or function () = T usage
You can write:
type Initializer<T> = T extends any ? (T | (() => T)) : never
function correct<T>(arg: Initializer<T>): T {
return typeof arg === 'function' ? arg() : arg // works
// arg is Initializer<T> & Function in the true branch
}
const r1 = correct(2) // const r1: 2
const r2 = correct(() => 2) // const r2: number
In the original version, arg
is resolved to (() => T) | (T & Function)
in the true branch. TS apparently can't recognize for this union function type, that both constituents are callable. At least in above version, it is clear for the compiler that you can invoke arg
after a function check.
Might also be worth to create a github issue for this case in the TypeScript repository - in my opinion T & Function
should represent some (wide) type of function.
Related Topics
Shared-Memory Ipc Synchronization (Lock-Free)
Why Do Compilers Allow String Literals Not to Be Const
Std::Shared_Ptr Thread Safety Explained
How to Read File Content into Istringstream
Difference in Performance Between Msvc and Gcc for Highly Optimized Matrix Multplication Code
Number of Combinations (N Choose R) in C++
Std::Sort Does Not Always Call Std::Swap
"No Newline at End of File" Compiler Warning
What Are the Signs of Crosses Initialization
Are Inner Classes in C++ Automatically Friends
What Is "Pch.H" and Why Is It Needed to Be Included as the First Header File
Clarification Needed Regarding Getchar() and Newline
Convert Between String, U16String & U32String
Iterating C++ Vector from the End to the Beginning
When Is Overloading Pass by Reference (L-Value and R-Value) Preferred to Pass-By-Value
Is the Pointer Guaranteed to Preserve Its Value After 'Delete' in C++