我试图运行一个基于的程序constexpr.
码:-
#include <iostream>
using namespace std;
int main()
{
const int i = 10;
constexpr int j = 10;
constexpr int val1 = i;
constexpr int val2 = j;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在我所遵循的书中,提到如果将const分配给constexpr变量,那就是错误.
但我的程序编译没有任何投诉.
我错过了什么吗?
我正在使用c ++模板测试以下代码.我使用int和float以及函数模板编写了一个square函数.
#include <iostream>
using namespace std;
int square (int a){
cout << "int function" << endl;
return a*a;
};
float square (float a){
cout << "float function" << endl;
return a*a;
};
template <typename T>
T square (T x){
cout << "template function" << endl;
return x*x;
}
int main(){
cout << square<int>(5) << endl;
cout << square<float>(5.5) << endl;
cout << square(5) << endl;
cout << square(5.5) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是
template function
25
template function …Run Code Online (Sandbox Code Playgroud)