C ++模板代码的语法和语义是什么? [英] What are the syntax and semantics of C++ templated code?

查看:154
本文介绍了C ++模板代码的语法和语义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

template<typename T, size_t M, size_t K, size_t N, typename std::enable_if_t<std::is_floating_point<T>::value, T> = 0>
void fastor2d(){//...}

我复制了这一行代码从cpp-reference(仅 std :: enable_if 部分,我确实需要 T 和所有三个 size_t ),因为我只想在使用float_types时才使用此函数...因此无法编译。

I copied this line of code from cpp-reference(only the std::enable_if part, i do need T and all three of the size_t's), because i would like to use this function only when floating_types are used on it ... it does not compile.

有人可以向我解释为什么,它甚至做什么?当我在使用它时,您以后如何调用此函数?

Could somebody explain to me, why, and what it even does? While i am at it, how do you call this function afterwards?

这里的每个教程或问题都被答案轰炸了,这很棒,但是对于那些不了解正在发生的事情,即使是那些并没有真正的帮助。(对不起,如果可能有点激动或激进)

Every tutorial or question here on SO gets bombed with answers, and that is great, but to someone who does not understand jacks*** of what is happening, even those are not really helpful.(sry, if possibly slightly agitated or aggressive)

编辑:我非常感谢所有人到目前为止的答案,我意识到我的措辞可能有点不对...我了解模板参数是什么,并且知道运行时和编译时等之间的区别,但是我只是无法很好地理解 std :: enable_if

i greatly appreciate all answers as of now, i realize that my wording might have been a bit off ... i understand what a template parameter is, and know the difference between runtime and compiletime etc, but i just cant get a good grasp of the syntax behind std::enable_if

EDIT2:

template<typename T, size_t M, size_t K, size_t N, typename = std::enable_if_t<std::is_integral<T>::value>>
void fastor2d(){
    Fastor::Tensor<T,M,K> A; A.randInt();
}

这实际上是我唯一需要更改的东西。请注意random()部分

This is literally the only thing i need changed. Notice the random() part

template<typename T, size_t M, size_t K, size_t N, typename = std::enable_if_t<std::is_floating_point<T>::value>>
void fastor2d(){
    Fastor::Tensor<T,M,K> A; A.random();
}


推荐答案

我将尝试解释

模板参数是编译时参数(它们在运行时不会更改,您的申请时间)。函数参数是运行时的,并且具有内存地址。

Template arguments are compile time arguments (they do not change during the run-time of your application). Function arguments are run-time and have a memory address.

调用此函数将如下所示:

Calling this function would look something like this:

fastor2d<Object, 1, 2, 3>();

在<>括号中,您会看到编译时参数或更准确地说是模板参数,并且在这种情况下,该函数在()中使用0个运行时参数。最后一个编译时参数具有默认参数,该默认参数用于检查函数是否应该完全编译(enable_if类型)。如果您想更清楚地知道应该启用什么功能,应该搜索术语SFINAE,这是一种模板元编程技术,用于确定函数或类是否应该存在。

In the <> brackets you see the compile-time arguments or more accurately the template parameters, and the function in this case takes 0 runtime arguments in the () brackets. The last compile time argument has a default argument which is used to check whether the function should compile at all (enable_if type). If you want to know more clearly what enable if does you should search for the term SFINAE, which is a template metaprogramming technique used to determine whether a function or class should exist or not.

这是SFINAE的简短示例:

Here is a short SFINAE example:

template<typename T, typename = std::enable_if_t<std::is_floating_point<T>::value>>
void function(T arg)
{
}

function(0.3f);    //OK
function(0.0);     //OK double results in std::is_floating_point<double>::value == true
function("Hello"); //Does not exist (T is not floating point)

第三个函数调用失败的原因是因为该功能不存在。这是因为当作为模板参数传入的编译时布尔值为false时,启用if导致该函数不存在。

The reason the third function call fails, is because the function does not exist. This is because the enable if caused the function not to exist when the compile-time bool that is passed in as its' template argument is false.

std::is_floating_point<std::string>::value == false

Do请注意,很多人都认为SFINAE语法太可怕了,并且随着C ++ 20概念和约束的引入,不再需要很多SFINAE代码。

Do note that a lot of people agree that the SFINAE syntax is horrible and that a lot of SFINAE code will not be necessary anymore with the introduction of concepts and constraints in C++ 20.

这篇关于C ++模板代码的语法和语义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆