Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of intrinsic type with () in C++

Tags:

c++

What does it mean when an intrinsic type has the parenthesis operators with it.

e.g. int() or float()

Is it like a default constructor or does it have a different meaning for intrinsic types?

----edit----

To add more context:

In my school textbook for C++, I am on a chapter about Templates. A template being used for demonstration is a Table template (a 2D array). One of the functions for Table is a resize method to change the dimensions of the table and is also used in some constructors used for Table. The constructor in question and the resize method are:

template <typename T> 
Table<T>::Table<T>(int m, int n) 
{  
    mDataMatrix = 0;  
    mNumRows    = 0;  
    mNumCols    = 0;  
    resize(m, n, T()); 
}

and

template <typename T> 
void Table<T>::resize(int m, int n, const T& value) 
{  
    // Destroy the previous data.
    destroy();   

    // Save dimensions.  
    mNumRows = m;  
    mNumCols = n;   

    // Allocate a row (array) of pointers.  
    mDataMatrix = new T*[mNumRows];   

    // Now, loop through each pointer in this row array.  
    for(int i = 0; i < mNumRows; ++i)  
    {   
        // And allocate a column (array) to build the table.   
        mDataMatrix[i] = new T[mNumCols];    

        // Now loop through each element in this row[i]   
        // and copy 'value' into it.   
        for(int j = 0; j < mNumCols; ++j)    
            mDataMatrix[i][j] = value;  
    } 
} 

In the constructor definition, resize's third argument is T() (and I assume T becomes whatever the specified type for the template is).

In the definition for resize, T() is used for the value argument to assign a default value to the elements in the table.

From some of the earlier answers, this is zero-initialization. I assume that means the value is 0 for each element in the table (or some equivalent of 0 if the type is a string, I guess). Is this correct?

like image 702
Dr Negative Avatar asked Oct 16 '16 18:10

Dr Negative


People also ask

What is intrinsic function in C?

Intrinsics are functions that the compiler provides. An intrinsic function has the appearance of a function call in C or C++, but compilation replaces the intrinsic by a specific sequence of low-level instructions.

What do you mean by intrinsic function?

An intrinsic function is a function that performs a mathematical, character, or logical operation, and thereby allows you to make reference to a data item whose value is derived automatically during execution.

What is an intrinsic function in C++?

Normally, "intrinsics" refers to functions that are built-in -- i.e. most standard library functions that the compiler can/will generate inline instead of calling an actual function in the library. For example, a call like: memset(array1, 10, 0) could be compiled for an x86 as something like: Intrinsics like this are purely an optimization.

What is the meaning of intrinsic?

Definition of intrinsic 1a : belonging to the essential nature or constitution of a thing the intrinsic worth of a gem the intrinsic brightness of a star b : being or relating to a semiconductor in which the concentration of charge carriers is characteristic of the material itself instead of the content of any impurities it contains

What is the difference between an intrinsic function and an inline function?

In short, the practical difference between an intrinsic funciton and an inline function is that intrinsic functions are "present" even if you have not #include the needed header file which contains the function declaration. For an inline function, the header file with the function declaration must be #include 'd (or otherwise declared) first.

How are intrinsic functions implemented in Java?

Instead, the intrinsic function is implemented by the compiler in lieu of a function call. In the example of strncpy (), the byte-copying code is emitted directly at the place where strncpy () is invoked. Similar to this strncpy () example, every intrinsic function is implemented directly as in-line code if required constraints are met.


2 Answers

That is value initialization.

e.g. auto x = int(); means int x = 0

In the example provided T() will create an object of type T and pass it as a parameter.

You could also write:

resize(m, n, T{}); 
like image 157
wally Avatar answered Oct 13 '22 08:10

wally


Acutally, your code does something different than when you're declaring just a int or a float. It zero initialize the numbers.

example, those three lines are equivalent:

int a = int();

int a = int{};

int a{};

All those three are zero initialized.

like image 3
Guillaume Racicot Avatar answered Oct 13 '22 07:10

Guillaume Racicot