compile-time

A standard way for getting variable name at compile time

牧云@^-^@ 提交于 2020-12-02 23:21:06
问题 Is there some way in C++11 or higher to achieve a similar behavior to: int some_int; std::string x=variable_name<some_int>::value; //Theoretical code std::cout << x; Result should be: some_int If not, is there a compiler specific way to do it? I am targeting MSVS. 回答1: You ask: Is there some way in C++11 or higher to achieve a similar behavior to: int some_int; std::string x=type_name<some_int>::value; //Theoretical code std::cout << x; Result should be: some_int Yes, you can just use the

Why is char[][] = {{…}, {…}} not possible if explicitly given a multidimensional array?

一曲冷凌霜 提交于 2020-07-27 05:42:17
问题 I went through this article. I understand the rules explained but I am wondering what exactly blocks the compiler from accepting the following syntax when defining a constant multi-dimensional array and directly initializing it with known values of given type: const int multi_arr1[][] = {{1,2,3}, {1,2,3}}; // why not? const int multi_arr2[][3] = {{1,2,3}, {1,2,3}}; // OK error: declaration of 'multi_arr1' as multidimensional array must have bounds for all dimensions except the first What

What are 'constexpr' useful for?

﹥>﹥吖頭↗ 提交于 2020-06-09 16:53:29
问题 I really can't find any use of it. My first idea was that I could use it to implement 'Design by Contract' without using macros like this: struct S { S(constexpr int i) : S(i) { static_assert( i < 9, "i must be < 9" ); } S(int i); //external defintion char *pSomeMemory; }; But this wouldn't compile. I thought we could also use it to reference same-variable without the need of additional memory to be created when we want to avoid the get/setters in order to make instances to one member from

Assign an array or an integer without knowing its nature in the function code (but compiler knows)

帅比萌擦擦* 提交于 2020-03-24 14:15:50
问题 I'm looking for something like this snippet. I expect it to know, at compile time, wether it is dealing with an array or not, and avoid the following errors. #include <stdio.h> #define IS_ARRAY(x,type) _Generic((&x), \ type (*)[]: 1, \ default: 0) #define GENERIC_ASSIGN(arg,type) if(IS_ARRAY(arg,type)){arg[0] = 1; arg[1] = 2;}else{arg = 2;} int main(void) { int foo = 0; int bar[10] = {0}; GENERIC_ASSIGN(bar,int); //--> error: assignment to expression with array type GENERIC_ASSIGN(foo,int); /

how to reduce time taken

隐身守侯 提交于 2020-03-04 20:04:29
问题 #include <iostream> using namespace std; void rotateByOne(int arr[], int n) { int x = arr[0]; for (int y = 0; y < n - 1; y++) { arr[y] = arr[y + 1]; } arr[n - 1] = x; } int main() { int n, d; cin >> n >> d; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } while (d != 0) { rotateByOne(arr, n); d--; } for (int i = 0; i < n; i++) { cout << arr[i] << " "; } return 0; } How Do i Reduce the compile time of this code which is written to take an array input of n integers and rotate array

how to reduce time taken

僤鯓⒐⒋嵵緔 提交于 2020-03-04 20:03:26
问题 #include <iostream> using namespace std; void rotateByOne(int arr[], int n) { int x = arr[0]; for (int y = 0; y < n - 1; y++) { arr[y] = arr[y + 1]; } arr[n - 1] = x; } int main() { int n, d; cin >> n >> d; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } while (d != 0) { rotateByOne(arr, n); d--; } for (int i = 0; i < n; i++) { cout << arr[i] << " "; } return 0; } How Do i Reduce the compile time of this code which is written to take an array input of n integers and rotate array

Call hidden (non virtual) method of derived class from base

☆樱花仙子☆ 提交于 2020-01-24 19:25:48
问题 Is there a way (templates, macros anything else) to substitute a call to hidden_in_derived from common method at compile time , so that instance of Derived calls it's own hidden_in_derived ( without making hidden_in_derived virtual in Base) ? #include <iostream> class Base { public: void common() { // some calls to other methods hidden_in_derived(); // yet some calls to other methods } void hidden_in_derived() { std::cout << "A" << std::endl; } }; class Derived : public Base { public: void

Call hidden (non virtual) method of derived class from base

旧城冷巷雨未停 提交于 2020-01-24 19:25:06
问题 Is there a way (templates, macros anything else) to substitute a call to hidden_in_derived from common method at compile time , so that instance of Derived calls it's own hidden_in_derived ( without making hidden_in_derived virtual in Base) ? #include <iostream> class Base { public: void common() { // some calls to other methods hidden_in_derived(); // yet some calls to other methods } void hidden_in_derived() { std::cout << "A" << std::endl; } }; class Derived : public Base { public: void