小编Már*_*ldi的帖子

为什么 require 子句中的否定表达式需要括号?

以下代码是 -clause 的用法示例requires

#include <type_traits>

template <typename T>
  requires std::is_integral_v<T>
void take_integral(T value);
Run Code Online (Sandbox Code Playgroud)

它接受一个计算结果为bool值的表达式(std::is_integral_v<T>在本例中),并按预期工作。但是,当使用运算符对此类表达式求反时!,会导致编译错误:

#include <type_traits>

template <typename T>
  requires !std::is_integral_v<T>
void take_integral(T value);
Run Code Online (Sandbox Code Playgroud)

来自 GCC 的诊断:

<source>:4:12: error: expression must be enclosed in parentheses
    4 |   requires !std::is_integral_v<T>
      |            ^~~~~~~~~~~~~~~~~~~~~~
      |            (                     )
Compiler returned: 1
Run Code Online (Sandbox Code Playgroud)

来自 Clang 的诊断:

<source>:4:12: error: parentheses are required around this expression in a requires clause
  requires !std::is_integral_v<T>
           ^~~~~~~~~~~~~~~~~~~~~~
           (                     )
1 error generated.
Compiler returned: …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++-concepts c++20

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

为什么不能从std :: addressof <int>解析模板参数?

Clang和GCC(MSVC除外)在std::addressof<int>作为参数传递给模板函数时无法解析模板参数.以下是此类错误的示例:

std::vector<int> v{1,2,3,4,5};
std::vector<int*> pv(iv.size());
std::transform(v.begin(), v.end(), pv.begin(), std::addressof<int>);
Run Code Online (Sandbox Code Playgroud)

铛:

<source>:8:5: error: no matching function for call to 'transform'
    std::transform(iv.begin(), iv.end(), piv.begin(), std::addressof<int>);
    ^~~~~~~~~~~~~~
/opt/compiler-explorer/clang-5.0.0/bin/../include/c++/v1/algorithm:2028:1: note: candidate template ignored: couldn't infer template argument '_UnaryOperation'
transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
^
Run Code Online (Sandbox Code Playgroud)

GCC:

/opt/compiler-explorer/gcc-7.2.0/include/c++/7.2.0/bits/stl_algo.h:4295:5: note:   template argument deduction/substitution failed:
<source>:8:74: note:   could not resolve address from overloaded function 'addressof<int>'
     std::transform(iv.begin(), iv.end(), piv.begin(), std::addressof<int>);
                                                                          ^
Run Code Online (Sandbox Code Playgroud)

如果参数是a std::addressof,那么该错误将是有意义的,因为UnaryOperator模板参数将是不明确的.但是,编译器不需要演绎什么Tstd::addressof<int>,我会在这里不放过任何歧义.

这是我期望的一个有效例子(编译Clang 5和GCC 7.2):

template …
Run Code Online (Sandbox Code Playgroud)

c++ templates stl

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

如何从Maybe中提取IO()并执行此操作?

我有这个代码:

trick = Just (putStrLn "Hello?")
Run Code Online (Sandbox Code Playgroud)

我想IO ()从Maybe上下文中解开它并调用它.

main = do foo <- trick
          foo
Run Code Online (Sandbox Code Playgroud)

但是,这会引发错误:

Couldn't match type ‘IO’ with ‘Maybe’
Expected type: Maybe ()
  Actual type: IO ()
Run Code Online (Sandbox Code Playgroud)

我怎么能解决这个问题?

monads haskell

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

如何在函数参数中通过引用获取数组?

如果数据类型是typedef,如何通过引用传递数组.我正在学习c ++,我阅读了call-by-reference的概念,但是当我按照那个实现时 - 我收到了一个错误(在代码之后粘贴在下面).请问,任何人都可以解释发送数组作为参考调用的最佳方式吗?

#include <iostream>
#include <vector>
using namespace std;

typedef unsigned long ulong;

ulong fib_dynamic(ulong n, ulong &memo[]){
  if(n < 2) return 1;
  if(memo[n] == 0){
    memo[n] = fib_dynamic(n-1, memo) + fib_dynamic(n-2, memo);
  }
  return memo[n];
}

ulong fib_iterative(ulong n){
  ulong fib[n+1];
  fib[0] = 1;
  fib[1] = 1;
  for(int i=2; i<n; i++) {
    fib[i] = fib[i-1] + fib[i-2];
  }
  return fib[n-1];
}

int main(){

  ulong n;
  cout << "Welcome to Fib Calculator\nEnter the n:";
  cin >> n;
  ulong …
Run Code Online (Sandbox Code Playgroud)

c++

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

标签 统计

c++ ×3

templates ×2

c++-concepts ×1

c++20 ×1

haskell ×1

monads ×1

stl ×1