相关疑难解决方法(0)

C++14:decltype(auto) 和“auto”作为返回类型有什么区别?

我有一个像这样的简单程序:

template<class T1,class T2>
decltype(auto) Add(T1&& t1,T2&& t2)
{
    return t1+t2
}

int main(){
    int i=Add(1,2);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它使用 clang++ -std=c++14 进行编译。我发现我可以消除“decltype”关键字,如下所示:

template<class T1,class T2>
auto Add(T1&& t1,T2&& t2)
{
    return t1+t2;
}
int main(){
    int i=Add(1,2);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

不过,没问题。所以我的问题是,在不同的场景中,“decltype(auto)”和“auto”何时有所不同?我只是猜测“decltype(auto)”是为“auto”无法工作时更复杂的情况而设计的?但具体是什么,请指教。

多谢。

c++ return decltype auto c++14

6
推荐指数
0
解决办法
252
查看次数

使用新模板参数递归调用模板化函数

我试图在c ++中实现一些功能构造.想要实现一个功能,可以将任意级别的列表列表缩小.

template<typename T, typename R>
struct Fold{
    typedef R(*func)(T, R);
};


template<typename T>
T head(std::list<T> const& list) {
    return list.front();
}

template<typename T>
std::list<T> tail(std::list<T> list) {
    list.pop_front();
    return list;
}

template<typename T>
std::list<T> cons(T head, std::list<T> tail){
    tail.push_front(head);
    return tail;
}

template<typename T, typename ACCUM>
ACCUM foldl(typename Fold<T, ACCUM>::func function, ACCUM accum, std::list<T> list) {
    if(list.empty())
        return accum;

    return foldl(function, (*function)(head(list), accum), tail(list));
}

template<typename T, typename ACCUM>
ACCUM foldr(typename Fold<T, ACCUM>::func function, ACCUM accum, std::list<T> list) { …
Run Code Online (Sandbox Code Playgroud)

c++ templates functional-programming visual-c++

5
推荐指数
2
解决办法
821
查看次数

C++ 11/14并返回(...)vs return

C++中,您可以编写一个返回语句,如下所示:

return ( ... );
Run Code Online (Sandbox Code Playgroud)

这与更受欢迎的不同:

return ... ;
Run Code Online (Sandbox Code Playgroud)

特别是第一个版本返回包含该return语句的函数堆栈本地的东西的地址/引用.

既然为什么某些东西想要返回一个对那些在那时没有生命的东西的引用呢?

这个成语有什么用例?考虑到C++ 11和C++ 14的新流行语和功能,有不同的用法吗?

c++ decltype c++11 type-deduction c++14

3
推荐指数
3
解决办法
1018
查看次数

就效率而言,C ++ 11是否比C ++ 03快?

我在贸易公司工作,在这里延迟很重要。分配给我的项目是使用c和c ++ 98部分混合开发的,但是我相信我们可以使用C ++ 11进行相同的项目而不会降低效率。正如我与年长者讨论的那样,他们说您应该坚持使用C和c ++ 03,因为它们在微观层面上比C ++ 11更有效。谁能强调我?如果我使用C ++ 11,我会得到更好的结果吗?

c++ low-latency c++11 c++03

3
推荐指数
1
解决办法
1226
查看次数

如何验证返回的`auto`变量是否为引用

在进行代码维护时,我找到了这样的代码:

auto networkEntry = _networkEntries[key];
networkEntry.port = port;
networkEntry.scope = scope;
Run Code Online (Sandbox Code Playgroud)

用于的地图数据类型_networkEntries有两个重载版本operator[]:

template<class T>
class Map {
    // ... simplified STD compatible container ...
    T & Map::operator[](const Key & key);
    const T Map::operator[](const Key & key) const;
};
Run Code Online (Sandbox Code Playgroud)

地图中使用的数据类型很简单struct.

现在我只是想知道,返回的值auto可以是数据结构的副本,也可以是对数据结构的引用.如果返回副本,则分配不会影响映射中的存储值.

我对这个案子有三个相关的问题:

  • 我可以知道或测试operator[]使用的是哪个版本?
  • 哪些C++规则适用于此处?
  • 有没有办法,使用auto,以确保使用参考?

c++ reference auto c++11

1
推荐指数
1
解决办法
108
查看次数

通用“sqr”函数

我试图弄清楚如何为 sqr 运算编写“完全通用函数”(它实际上可以是乘法、除法、加法,并不重要)。

考虑下面的代码

#include <iostream>

struct A
{
    int val = 2;

    A() = default;
    A(const A&) = delete; // To make sure we do not copy anything
    A(A&& a) = delete; // To make sure we do not move anything
    auto operator=(auto) = delete; // To make sure we do not assign anything
    // This is important part, we do not want to create a new object on each multiplication.
    // We want just to update the old …
Run Code Online (Sandbox Code Playgroud)

c++ c++14 c++17 c++20

0
推荐指数
1
解决办法
3079
查看次数