difference between static T const and static const T - iphone

Assuming that T is an arbitrary type and we're talking about Objective-C. Then what is the difference between
static T const x = ....
and
static const T x = ....
?

They are the same.
A little background about const:
Objective-C, just like C, uses the Clockwise/Spiral Rule. In a case where you have only modifiers at the left side of the variable (of foo) you read stuff going from right to left:
int * const * var;
is read: var is a pointer to a constant pointer to integer.
However const has the same effect when it's before or after the first type:
int const var; // constant integer
const int var; // constant integer, same as above
static, instead, affects the whole variable: it's the variable to be static, not the element pointed by it or anything else: a pointer to integer can be used both to point to a static integer, or to an automatic integer without distinction, and C doesn't provide a syntax to declare a pointer able to point to static variables (or to automatic variables) only.
static int * x; // x is a static pointer to integer
int static * y; // as above y is a static pointer to integer
int * static z; // THIS SYNTAX IS NOT ALLOWED
// but if it was, it would mean the same thing
For this reason I prefer to place it before any const modifier, but it doesn't actually matter.
static const int * const * x; // this is what I do

I believe the distinction is only important for pointer types; for example 'char *'.
const char *p means that the buffer pointed to by p cannot be updated but pointer p itself can be changed (typically incremented with p++), for example:
void f(const char *p)
{
while (*p != '\0')
{
putc(*p);
p++;
}
}
char * const p means that the buffer pointed to by p can be updated but pointer p cannot be changed.
EDIT Thanks for JeremyP for the following comment:
It's quite common in Apple's APIs to define string constants (instead of using #defines), especially if the string is an NSString i.e. NSString * const kFoo = #"bar";

Related

Natvis has trouble parsing a typedef'd template in VSCode

I have this (partial) code:
template<typename BaseType, unsigned int IntegerBits, unsigned int FractionalBits = std::numeric_limits<BaseType>::digits - IntegerBits>
struct fixed_point
{
private:
template <int P, typename T=void> struct power2 { static const long long value = 2 * power2<P-1,T>::value;};
template <typename P> struct power2<0, P> { static const long long value = 1; };
BaseType m_value;
const static unsigned char integer_bits = IntegerBits;
const static unsigned char fractional_bits = FractionalBits;
public:
inline fixed_point(float value) : m_value((BaseType)(value >= 0.0f ? value * power2<FractionalBits>::value + 0.5f : value * power2<FractionalBits>::value-0.5f)) { };
}
Given
typedef fixed_point<int32_t,15,16> MYFIXED_POINT ;
When I try to visualize with a natvis like this
<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
<Type Name="fixed_point<*,*,*>">
<DisplayString>{(float)m_value / (float)(1 << $T3)}</DisplayString>
</Type>
<Type Name="Occupancy_Map::FLOAT" >
<DisplayString>{(float)m_value / (float)(1 << 16)}</DisplayString>
</Type>
</AutoVisualizer>
The top one doesn't visualize MYFIXED_POINT entirely (though it does seem to recognize this is the type of interest and will try to display things (but on the member variable lines, not the unexpanded line) and will pick up the 16 from the template (which I can tell by changing the display string to {$T3}).
It does work correctly with a fixed_point<int32_t,15,16> declared variable.
The bottom one works, but it doesn't have the template parameters, so T3 evaluates to void, which is why the hardcoded 16 is there.
I've tried fiddling with AlternativeName, but that didn't seem to help me.
Any ideas?

Error: passing 'const xxx' as 'this' argument discards qualifiers

I am trying to implement bigint class in c++, it's not completed yet, i have encountered some errors that i am unable understand.
I have erased all other functions (as they are unnecessary in this case)
and karatsuba is not yet completed (but that should't pose a problem in this case).
In the multiply function (overloaded * ) my compiler gives an error:
passing 'const BigInt' as 'this' argument discards qualifiers [-fpermissive]
at line
ans.a = karatsuba(n,m);
I understand that this would occur when i am trying to change a constant object or object passed to a constant function, in my case i am merely creating a new vector and passing it to karatsuba function.
Removing const from overloded * gets rid of this error.
So,does this mean that a constant function can't change anything at all? (including local variables?)
class BigInt {
typedef long long int ll;
typedef vector<int> vi;
#define p10 1000000000;
#define range 9
vi a;
bool sign;
public:
BigInt operator * (const BigInt &num) const
{
vi n(a.begin(),a.end()),m(num.a.begin(),num.a.end());
BigInt ans;
ans.sign = !(sign ^ num.sign);
while(n.size()<m.size()) n.push_back(0);
while(n.size()>m.size()) m.push_back(0);
ans.a = karatsuba(n,m);
return ans;
}
vi karatsuba(vi a,vi b)
{
int n = a.size();
if(n <= 16)
{
// some code
}
// some code
return a;
}
};
Ok so after googling a bit more, i realized that this pointer is implicitly passed to the oveloaded * and then on to karatsuba (as it is a member function of the class), and as karatsuba is not a constant function, there is no guarantee that it won't change the object contents, hence this error is triggered.
One solution is to declare karatsuba as static, as static member functions don't receive this pointer (they can even be called with out a class object simply using :: operator) , read more about them from here Static data members and member functions.
All that is needed to be changed is :-
static vi karatsuba(vi a,vi b)
{
int n = a.size();
if(n <= 16)
{
// some code
}
// some code
return a;
}

Systemverilog const ref arg position when constructing an object

I am creating a class that needs a reference to the test bench's configuration object. Since the configuration must be intact throughout the simulation, I pass it as a const ref object. Here is a sudo code that I want to run:
class tb_config;
int unsigned rate;
int unsigned chnls[];
const int unsigned nb_chnls;
function new (int unsigned rate, int unsigned nb_chnls);
this.rate = rate;
this.nb_chnls = nb_chnls;
chnls = new[nb_chnls];
endfunction
endclass
class tx_phy;
static int phy_id;
tb_config cfg;
function new (int unsigned phy_id, const ref tb_config cfg);
this.phy_id = phy_id;
this.cfg = cfg;
endfunction
endclass
module test;
tb_config cfg = new(100, 4);
tx_phy phy = new( 1234, cfg);
endmodule
The code above works perfectly fine and it meets my expectation. But if I change the arguments in tx_phy::new to function new (const ref tb_config cfg, int unsigned phy_id); and pass the values to the constructor accordingly I get the following error in Cadence Incisive:
invalid ref argument usage because actual argument is not a variable.
Also same thing happens when I test it with Aldec in edaplayground: https://www.edaplayground.com/x/5PWV
I assume this is a language limitation, but is there any other reason for that??
The reason for this is because the argument kind is implicit if not specified. You specified const ref for the first argument, but nothing for the second argument, so it is also implicitly const ref. Adding input to the second argument declaration fixes this.
function new (const ref tb_config cfg, input int unsigned phy_id);
I also want to add const ref tb_config cfg is equivalent to writing
function new (tb_config cfg, int unsigned phy_id);
Both of these arguments are implicitly input arguments, which means they are copied upon entry.
A class variable is already a reference. Passing a class variable by ref means that you can update the handle the class variable has from within the function. Making the argument a const ref means you will not be able to update the class variable, but you can still update members of the class the variable references. There is no mechanism to prevent updating members of class object if you have a handle to it other than by declaring them protected or local.
The only place it makes sense to pass function arguments by ref in SystemVerilog is as an optimization when the arguments are large data structures like an array, and you only need to access a few of the elements of the array. You can use task ref arguments when the arguments need to be updated during the lifetime of the task (i.e. passing a clock as an argument).

MFC Classes CMAP

What I do not understand is the : “UINT&”. What does it mean in the context of the template
static CMap<CString, LPCSTR, UINT, UINT&> cXMLfields::fields_by_name;
static CMap<CString, LPCSTR, UINT, UINT&> cXMLfields::oopf_fields_by_name;
static CString friendly_name[XML_FIELDNUM];
static CString fields_by_id[XML_FIELDNUM];
static CString oopf_fields_by_id[OOPF_XML_FIELDNUM];
static void Build_map();
static void MoveItem(CListBox& src, CListBox& dst, int index);
CMap<CString,LPCTSTR, struct_sample,struct_sample> myMap;
struct_sample aTest;
aTest.a = 1;
aTest.b = 2;
aTest.c = 3;
myMap.SetAt("test",aTest);
struct_sample aLookupTest;
BOOL bExists = myMap.Lookup("test",aLookupTest); //Retrieves the
//struct_sample corresponding to "test".
I understand the theory, but my key is a String e.g “RFIDTAG1”.And my value is an unsigned integer. Example.UNIT is the Value to the Specific KEY ...for example {Age : 27} Where Age is the Key and 27 is the Value.I struggle when the value is an integer, I can understand if the value was a structure.So Can you use {Age: 27 } as the example and show me the code like below when using a structure
The & means pass by reference.
See this link.
Also here on MSDN.
ARG_VALUE
Data type used for VALUE arguments; usually a reference to VALUE.

Emulating 'return' in macro

Is there any standard compliant way to emulate 'return' with macro?
Currently, I'm trying to wrapping _alloca function to emulate variable length array on stack (which is supported in C99) with macro in C++. Since the _alloca function manipulates stack pointer, I think inline function isn't suitable solution for this time.
Below is the current code that I've written.
template <typename T>
inline void __placement_new_array(T arr[], const size_t size) {
assert(size > 0);
for (size_t i = 0; i < size; i++) {
new (&arr[i]) T;
}
}
template <typename T>
class __arraydtor
{
public:
__arraydtor(T arr[], size_t size) : arr_(arr), size_(size) {}
~__arraydtor() {
for (size_t i = size_ - 1; i != (size_t)(-1); i--) {
arr_[i].~T();
}
}
private:
T* arr_;
size_t size_;
};
#define stack_alloc(size) _alloca(size)
#define stacknew(ptr, type, size) \
ptr = static_cast<type*>(stack_alloc(sizeof(type) * size));\
__placement_new_array(ptr, size);\
__arraydtor<type> __##type##_dtor_instance(ptr,size)
...
type* pos;
stacknew(pos, type, size);
I think the code is fairly usable even for now (it works for most type at least in vs2005), but ultimately I want to acheive the macro that can be used like below -
pos = stacknew(type, size);
(Of course pos = stacknew type[size]; would be more cool but I don't think there is a way to acheive it with any C++ compiler)
Since the macro contains some declaration, emulating 'return' is impossible in the current form - it might be impossible or needs a different approach. But I'm lack of experience for using macro, I can not judge whether it's possible or not.
Also I want to note that above code is not safe when the ctor of the target array throws exception - I'd also be grateful if someone suggests a way to improve the macro.
you can use , operator:
v = (expr1, expr2); // evaluate both expressions, return rightmost