Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;模板元编程:constexpr函数_C++_Templates_Metaprogramming - Fatal编程技术网

C++ C++;模板元编程:constexpr函数

C++ C++;模板元编程:constexpr函数,c++,templates,metaprogramming,C++,Templates,Metaprogramming,我在看比亚恩·斯特劳斯塔普的演讲 在问答环节中,关于如何管理繁重的模板编程代码,他提到:“通过constack敷衍,基本上可以消除通过编写普通代码生成值的每个模板元编程” 康斯塔克的敷衍只是一种猜测 我可以问一下这种技术的正确术语是什么吗?这样我可以做一些后续阅读 更新:只需将标题修改为“constexpr function”。constexpr在C++11中添加的函数可以在编译时进行计算,并在模板元编程中用作模板参数。在C++11中,它们非常有限,并且(几乎)只能由一个return表达式组成。

我在看比亚恩·斯特劳斯塔普的演讲

在问答环节中,关于如何管理繁重的模板编程代码,他提到:“通过constack敷衍,基本上可以消除通过编写普通代码生成值的每个模板元编程”

康斯塔克的敷衍只是一种猜测

我可以问一下这种技术的正确术语是什么吗?这样我可以做一些后续阅读


更新:只需将标题修改为“constexpr function”。

constexpr
在C++11中添加的函数可以在编译时进行计算,并在模板元编程中用作模板参数。在C++11中,它们非常有限,并且(几乎)只能由一个
return
表达式组成。C++14使它们的限制性更小

例如,这是可能的:

constexpr std::size_t twice(std::size_t sz) {
    return 2 * sz;
}

std::array<int, twice(5)> array;
constexpr std::size\u t两次(std::size\u t sz){
返回2*sz;
}
std::数组;
而在C++11之前,需要使用模板“hacks”,例如:

template<std::size_t sz>
class twice {
public:
    static const std::size_t value = 2 * sz;
}

std::array<int, twice<5>::value> array;
模板
上课两次{
公众:
静态常数std::size_t值=2*sz;
}
std::数组;

例如,它可以用于在编译时以干净的方式生成值(如数学常数、三角查找表等)。

constepr
函数?请查看您是否可以获得他的讲话的文字记录。。您连续添加了太多来自同一演讲的问题!!可能是constexpr函数,我给出了一个例子,说明如何在我的示例中用constexpr函数替换模板元编程。我的例子是关于这个主题的一篇好文章。constack敷衍:(v)每个函数(敷衍)都有一个倒置的堆栈(constack)。还有,不是真话。@ShafikYaghmour谢谢你,伙计!