小编Mic*_*ang的帖子

是否有可能在C++中创建一个可以自行调用的实际函数类型?

我试图通过Y-combinator在C++中引用函数名来编写递归.但是,我无法在以下尝试中找出函数的类型:

#include <iostream>

using std::cin;
using std::cout;

template<class Function> unsigned long factorial1(Function self, unsigned long n) {
    return n ? n * self(self, n - 1) : 1;
}

unsigned long factorial(unsigned long n) {
    return factorial1(factorial1, n);
}

int main() {
    unsigned long n;
    cin >> n;
    cout << factorial(n) << '\n';
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译器不能推断出什么是Function,我也不能.然后我尝试了以下内容:

#include <iostream>

using std::cin;
using std::cout;

struct Factorial {
    template<class Function> unsigned long operator()(Function self, unsigned long n) const {
        return …
Run Code Online (Sandbox Code Playgroud)

c++ recursion

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

mysql中未来日期的日期时间值不正确

MySQL语句:

insert into `banners` 
    (`path`, `app_id`, `enabled`, `from_date`, `to_date`, `updated_at`, `created_at`) 
values 
    ('banners/example.png', 'com.example.ccp_attacker', 1, '2000-01-01 00:00:00', '2099-12-31 00:00:00', '2100-06-04 00:00:00', '2100-06-04 00:00:00')
Run Code Online (Sandbox Code Playgroud)

产生错误 #1292 - Incorrect datetime value: '2100-06-04 00:00:00' for column 'updated_at' at row 1

为什么 2100-06-04 00:00:00 不是有效时间?

mysql

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

SQL 未知是否与 NULL 相同?

我很困惑 UNKNOWN 在 SQL 中的 3 值逻辑中意味着什么。它实际上意味着NULL吗?NULL 和 UNKNOWN 是否可以在所有布尔上下文中互换?

sql null three-valued-logic

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

为什么在三元运算符的分支之间返回lambda会为某些lambda工作?

我想根据一些条件选择一个lambda,但是对于一些lambdas,编译器说lambdas的类型在三元运算符的分支之间不匹配.

以下代码编译:

int flag = 4;
auto result = flag % 2
    ? [](int x) {
        return x + x;
    }
    : [](int x) {
        return x * x;
    };
Run Code Online (Sandbox Code Playgroud)

但以下2个片段无法编译:

int flag = 4;
auto result = flag % 2
    ? [flag](int x) {
        return x + flag;
    }
    : [flag](int x) {
        return x - flag;
    };

auto result2 = flag % 2
    ? [](auto x) {
        return x + x;
    }
    : [](auto x) {
        return x …
Run Code Online (Sandbox Code Playgroud)

c++ lambda

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

为什么在C++中container.value_type是非法的

以下代码是非法的:

#include <vector>
#include <utility>

int main() {
    std::vector<std::pair<int, char> > v;
    v.push_back(v.value_type(0, 'a')); // *
}
Run Code Online (Sandbox Code Playgroud)

我必须改变*

v.push_back(decltype(v)::value_type(0, 'a'));
Run Code Online (Sandbox Code Playgroud)

使代码工作.

为什么标记*的行不允许?这个理由背后的原因是什么?

c++ language-lawyer

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

Haskell - 程序挂起

我现在正在学习Haskell.因为它是一种纯粹的功能性语言,"一切都是有价值的",我相信我可以计算我想要的任何东西,因为"一切都是有价值的"!

然而,考虑下面的程序,它试图找到满足条件a ^ n + b ^ n == c ^ n的最小整数元组(a,b,c)给定用户输入n,这是一个正整数:

func :: Integer -> (Integer, Integer, Integer)
func n = head $ filter (\(a, b, c) -> a ^ n + b ^ n == c ^ n) listOfTuples

listOfTuplesWith :: Integer -> [(Integer, Integer, Integer)]
listOfTuplesWith 1 = [(1, 1, 1)]
listOfTuplesWith x = [(a, b, x) | a <- [1 .. x - 1], b <- [1 .. x - 1]] ++
    [(a, x, b) | …
Run Code Online (Sandbox Code Playgroud)

haskell

-4
推荐指数
1
解决办法
245
查看次数