小编Ler*_*Cpp的帖子

Why is it not required to use typename for dependent types in the following case?

I have been reading about removing reference of a type, here.

It gives the following example:

#include <iostream> // std::cout
#include <type_traits> // std::is_same

template<class T1, class T2>
void print_is_same() {
  std::cout << std::is_same<T1, T2>() << '\n';
}

int main() {
  std::cout << std::boolalpha;

  print_is_same<int, int>();
  print_is_same<int, int &>();
  print_is_same<int, int &&>();

  print_is_same<int, std::remove_reference<int>::type>(); // Why not typename std::remove_reference<int>::type ?
  print_is_same<int, std::remove_reference<int &>::type>();// Why not typename std::remove_reference<int &>::type ?
  print_is_same<int, std::remove_reference<int &&>::type>();// Why not typename std::remove_reference<int &&>::type ?
} …
Run Code Online (Sandbox Code Playgroud)

c++ templates dependent-name type-traits c++11

10
推荐指数
2
解决办法
578
查看次数

我可以 std::forward 折叠表达式中的参数吗?

在 C++11 中,我们有可变参数模板,我们可以在其中使用std::forward参数,如以下代码所示

#include <utility>
#include <iostream>
#include <string>

void printVariadic()
{}
template<typename T, typename... Args>
void printVariadic(T&& arg, Args&&... args)
{
   std::cout << arg << "\n";
   printVariadic(std::forward<Args>(args)...); // here we forward the arguments
}
Run Code Online (Sandbox Code Playgroud)

但是,在 C++17 中,我们有折叠表达式(根据我的理解,它直到最后一个参数才进行递归函数调用)。

template<typename ... Args>
void print(Args ... args)
{
    ((cout << args << "\n"), ...); // did we here miss the part of `std::forward`?
}
Run Code Online (Sandbox Code Playgroud)

std::forward在在线示例中,当使用折叠表达式时,我看不到参数的 ing。

我可以在折叠表达式中转发参数吗?或者我们根本不需要它?

这可能是一个愚蠢的初学者问题,但我仍然无法在网上找到答案。

c++ c++11 fold-expression c++17

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

如何从迭代器中找到一对的类型?

我在这里以及模板编程方面都是新手。我有一本字典(意味着它可以是std::mapor std::vector<std::pair<type1, type2>>std::set<std::pair<, >>...。)我想写一个算法,该算法的作用类似于使用所传递容器的迭代器的标准库算法。

以下是想法。

#include <iostream>
#include <algorithm>
#include <type_traits>
#include <vector>
#include <array>
#include <map>
#include <set>
#include <iterator>

// two different types
enum EnumA { one, two, three, four, five, six};
enum EnumB { one,      three, four,       six};

//                   TypeA TypeB
using map = std::map<EnumA, EnumB>;
           // or std::vector<std::pair<EnumA, EnumB>>
           // or std::set<std::pair<EnumA, EnumB>>
           // or std::array<std::pair<EnumA, EnumB>, 3>

const  map itemMap{       // the map
   {EnumA::one, EnumB::one}, 
   {EnumA::three, EnumB::three}, 
   {EnumA::six, EnumB::six}, 
}; …
Run Code Online (Sandbox Code Playgroud)

c++ generic-programming c++11

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