How to set automatically optimize code of template function in gcc? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have wondered about template optimizing. Look at the sample code:
....
template <class T>
T AReallyBigFunction(T); //size = a (MB)
....
int main(){
....
short number1;
int number2;
long number3;
cout << AReallyBigFunction(number1) << endl;
cout << AReallyBigFunction(number2) << endl;
cout << AReallyBigFunction(number3) << endl;
....
}
The question is: All three of number1, number2 and number3 always give the correct result with AReallyBigFunction<long>, but I don't want to notice to template parameter long and I don't want to multiply unneccessary program size, Does gcc/g++ can do it?
EDIT
size of AReallyBigFunction<T> equal to a MB for each class T. So, If call AReallyBigFunction with three various integer type, total size is 3*a MB. If I always call AReallyBigFunction<long> (with template parameter class T has been specified to long), the size is a MB. I want the compiler always calls AReallyBigFunction<long> although I just write AReallyBigFunction (without specifying class T, with any integer value (short, int or long)). My question is: does gcc/g++ has any solution (compiling mode) to respond me?

If the comment by #DrewDormann is correct interpretation of the question, then you can use the following trick to reduce code size.
Define function overloads that take an int and short and call the function template using long.
int AReallyBigFunction(int in)
{
long res = AReallyBigFunction<long>(in);
return static_cast<int>(res);
}
short AReallyBigFunction(short in)
{
long res = AReallyBigFunction<long>(in);
return static_cast<short>(res);
}

Related

Is it okay if we assign const integer a value like this? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I wanted to assign a value to a constant but as it is constant, we cant assign value to it through cin. So I came up with this idea. Is it okay if we use it like this?
{
int data = 0;
cout << "enter number of students"<<endl;
cin>>data;
const int number = data;
cout << number<<endl;
}
This is fine. const values do not need to be compile time constants. They just can't be changed after they are defined.
Similarly you could do:
int getNumberFromStdin() {
int data;
cin >> data;
return data;
}
int main() {
const int number = getNumberFromStdin();
}
Yes, because it is not an assignment; it is a declaration.
Despite looking quite similar, the two constructs are different: assignment changes something that is already declared, while the declaration sets up something new, and optionally gives it a value. The value does not need to be hard-coded, though: it can be a result of some processing, as is the case in your example.
Naturally, assignment is not allowed for constants, because they already have a value.

Memory Efficient: Templates vs Basic data types [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Is the use of templates better than basic data types in terms of memory allocation and management?
Just in short:
templates - a way to write code once in a generic way, and in compilation time the compiler will generate code according to the template, if you used the templatic code.
example:
#include <iostream>
template<typename T>
T MultiplyByFive(T _val)
{
return _val * 5;
}
int main()
{
std::cout << MultiplyByFive(5) << " " << MultiplyByFive(5.5) << std::endl;
return 0;
}
In this example, the compiler will generate two MultiplyByFive functions. One for integer and one for double. The output will therefore be:
25 27.5
That's because these functions have been called. Now we have two function in the code (generated by the compiler)
int MultiplyByFive(int _val)
{
return _val * 5;
}
double MultiplyByFive(double _val)
{
return _val * 5;
}
We didn't code them directly, but the compiler did according to our template.
Memory allocation has little to do with template. Dynamic memory allocation is determined in run time (in c++ by the new operator). Static and local variable are determined in compile time, but it has nothing to do with generating code.
If I didn't understand the question, you're more then welcome to clarify.

C++ convert *string to int [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Okay So i have this function
long valid(const char* str)
{
int temp = atoi(str)
return long;
}
`str = 9789070002046` //13 digit long
the function returns some random 10 digit number.I have also tried STOI stringstream
#shivam-gupta hi buddy,
You maybe have been quite new to C/C++ programming is that correct? Some people at the forum require you to make explain what your problem is in detail also with the right C++ syntax.
I know what you mean, and it had been a main problem with me in the beginning when I tried to learn C/C++. The function that you might be looking at is atol() not atoi(). From my point of view it is better to convert the string to double, then cast it to a long/integer variable.
The code I used to complete your snippet is this
#include <iostream>
long valid(const char* str);
int main()
{
long test;
const char *str = "9789070002046"; //13 digit long
test = valid(str);
std::cout << test << " sizeof test is " << sizeof(test) << std::endl;
return 0;
}
long valid(const char* str)
{
long temp = (long)atol(str);
return temp;
}
Enjoy coding C++! And remember, life is not too short to learn C++!

program to view addresses of elements in an array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
help plz its showing invalid indirection
i used it to find the location or memory addresses of elements in the array b
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int *ptr;
int b[]={1,0,2,3,4,5,6,7,8,9};
ptr=b;
for(int i=0;i<10;i++)
cout<<ptr[i]<<" "<<*b[i];
}
In order to print the address of the ith element in an array b, use
std::cout << b + i;
This will work in all cases except when b is an array of char, in which case you need to cast to void*
std::cout << static_cast<const void*>(b + i);
in place of iostream.h it should be iostream.
void main() ; it should be int main().
cout<<ptr[i]<<" "<<(b+i)<<endl;
Formatting by using endl in above line of code will make the result clear.
Your function should return an integer value.
return 0;

How to pass bitsets with different sizes to a function? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I want to pass bitsets to a function. What size should I assign to the bitset parameter bits in the function prototype if the bitsets have different sizes?
For example:
bitset<3> a;
bitset<4> b;
void ABC(bitset<size> bits){
...
}
ABC(a);
ABC(b);
You can templatize the function taking bitset as argument.
template <size_t bitsetsize>
void ABC(bitset<bitsetsize> a) {
...
}
This templatized function would be generated by compiler only when you use it somewhere in your code. If you use this function for bitsets of different sizes, separate functions would be instantiated for once for each size. So you should take care to avoid code depending on any local state variables (static variables local to function) as the function instances are different.
It is advisable to use a reference or constant reference to avoid object copy.
template <size_t bitsetsize>
void ABC(const bitset<bitsetsize> &a) {
...
}
An alternative which may not be fit for your requirements is to use std::vector<bool> instead of std::bitset if possible.
This is not possible with STL bitset.
std::bitset<N> template requires a fixed size in advance (at compile-time)
However, one way you can do this by using boost::dynamic_bitset
Something like following:
#include <iostream>
#include <boost/dynamic_bitset.hpp>
void ABC(boost::dynamic_bitset<> &a)
{
/* for (boost::dynamic_bitset<>::size_type i = 0;
i < a.size(); ++i)
std::cout << a[i]; */
std::cout << a << "\n";
}
int main()
{
std::size_t size= 5; // take any value for 'size' at runtime
boost::dynamic_bitset<> x(size); // all 0's by default
x[0] = 1;
x[1] = 1;
x[4] = 1;
ABC( x );
return 0;
}
See here