为什么不使用非类型模板参数实现std :: bind的占位符?

W.F*_*.F. 1 c++ templates template-meta-programming stdbind c++14

我知道这个问题相当理论化,但我认为如果将占位符定义为模板,例如:

namespace std { 
   namespace placeholders {
      template <size_t> struct placeholder { constexpr placeholder() {}; }; 
      template <size_t N> constexpr placeholder<N> _{};
   }
}
Run Code Online (Sandbox Code Playgroud)

用法:

std::bind(foo, std::placeholders::_<1>, std::placeholders::_<2>); 
Run Code Online (Sandbox Code Playgroud)

或者对于c ++ 11:

namespace std { 
   namespace placeholders {
      template <size_t> struct _ { }; 
   }
}
Run Code Online (Sandbox Code Playgroud)

用法:

std::bind(foo, std::placeholders::_<1>{}, std::placeholders::_<2>{});
Run Code Online (Sandbox Code Playgroud)

代码不会丢失任何清晰度,我们可以使用它做一些奇特的元编程.那么......为什么不std::bind使用非类型模板参数实现占位符?

Yak*_*ont 7

C++ 11中不存在变量模板,这是std::bind添加到语言中的位置.

这些_1名称很短,取自boost哪里std::bind开发.

您可以轻松编写自己的类似占位符.

namespace my_placeholders {
  template <int> struct placeholder { constexpr placeholder() {}; }; 
  template <int N> constexpr placeholder<N> _{};
}
namespace std {
  template<int N>
  struct is_placeholder< ::my_placeholders::placeholder<N> >:
    std::integral_constant<int, N>
  {};
}
Run Code Online (Sandbox Code Playgroud)

现在my_placeholders::_<1>是一个有效的std::bind占位符,相当于_1每一个重要的方式.

鉴于能够做到这一点,坦率地说std::bind,与lambda相比,它是多么令人讨厌,我可以看到没有人费心去实际添加这样的功能到标准的后期C++ 14.