ham*_*els 9 c++ templates metaprogramming
如何在编译时(实际编译和运行程序之前)知道/未知其参数之一的值的情况下专门化模板函数?
我怎么也弄不清楚.
想法1:
#include <type_traits>
#include <iostream>
int main(void){
int a; //value of a is not known at compile time
bool b = (a == a); //value of b is known at compile time.
std::is_assignable< constexpr bool, bool >::value
}
//g++ magic.cpp -std=c++14
//error: wrong number of template arguments (1, should be 2)
// std::is_assignable< constexpr bool, bool >::value
Run Code Online (Sandbox Code Playgroud)
想法2:
#include <type_traits>
#include <iostream>
int main(void){
const int a=1;
int b = (a == a);
std::cout << __builtin_constant_p (a) << std::endl;
std::cout << __builtin_constant_p (b) << std::endl;
}
//prints 0 and 0.
Run Code Online (Sandbox Code Playgroud)
好吧,我猜你指的是论证的类型,对吧?值对于部分模板专业化并不重要......
然后:这不能完成。
模板的参数类型必须在编译时已知。编译器还应该如何生成正确的代码?
同样,对于部分模板特化,出于同样的原因,必须在编译时知道类型。
| 归档时间: |
|
| 查看次数: |
201 次 |
| 最近记录: |