小编Евг*_*цын的帖子

C++ 参数包扩展与概念

如果取消注释行 #1 和注释行 #2,为什么会出现编译错误?演示: https: //godbolt.org/z/KW6dhsrKd

#include <utility>

template <typename, std::size_t> concept prefix = true;

template<std::size_t>
struct dummy { template<typename T> constexpr dummy(T){}; };

template <auto N>
consteval auto nth_element(auto... args) 
{
return [&]<std::size_t... Is>(std::index_sequence<Is...>) {
    //return [](prefix<Is> auto..., auto arg, auto...) { //compile error // #1
    return [](dummy<Is> ..., auto arg, auto...) { // OK                  // #2
    return arg;
    }(args...);
}(std::make_index_sequence<N>());
}

int main()
{
    static_assert(nth_element<0>(1, 2, 3) == 1);
    static_assert(nth_element<1>(1, 2, 3) == 2);
    static_assert(nth_element<2>(1, 2, 3) == …
Run Code Online (Sandbox Code Playgroud)

c++ templates language-lawyer template-meta-programming c++20

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

C++ 重载函数的地址

我有以下小的 C++ 代码示例,但我无法弄清楚为什么编译器以这种方式工作,尽管我花了很多时间研究 cppreference。我将不胜感激任何解释!神箭

#include <type_traits>

template<typename Tp>
struct depend_type
{
  constexpr static bool false_ = false;
};

template<typename Tp>
struct cont
{
  using x = void;

  static_assert(std::is_same_v<Tp, int>);
  // if uncomment, will be complie error -> 'cont<Tp>::x' instantiated with 'void (*p)(int) = func;'
//   static_assert(depend_type<Tp>::false_); 
};

template<typename Tp>
void func(Tp)
{
}

template<typename Tp>
typename cont<Tp>::x func(Tp);

int main(int /* argc */, char * /*argv*/[])
{
//  func(1); is ambiguous call
  void (*p)(int) = func; // why is …
Run Code Online (Sandbox Code Playgroud)

c++ function-templates overload-resolution template-meta-programming

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