小编DrL*_*ig3的帖子

C++使用lambda进行隐式构造函数调用,期望函数指针

我试图从lambda函数隐式构造一个对象.对象的构造函数将函数指针作为参数.但是代码[1]不能使用以下消息进行编译:

6 : <source>:6:5: note: candidate constructor not viable: no known conversion from '(lambda at /tmp/compiler-explorer-compiler117117-54-dfxyju.lkw98/example.cpp:22:14)' to 'Bar' (aka 'bool (*)()') for 1st argument
    Foo(Bar b) : m_b{b} {}
Run Code Online (Sandbox Code Playgroud)

但是标准规定lambda函数可以隐式转换为具有相同参数和返回类型的函数指针[2].这应该适用于此,因此我希望构造函数可以调用.

那么代码为什么不编译呢?谢谢你的解释!


[1]代码示例:

using Bar = bool(*)();

class Foo
{
public:
    Foo(Bar b) : m_b{b} {}
private:
    Bar m_b;
};

int main()
{   
    // working
    Foo f1 ( [](){ return true; });
    Foo f2 = Bar( [](){ return true; });

    // working implicit conversion
    bool(*tmp)() = []() { return true; }; …
Run Code Online (Sandbox Code Playgroud)

c++ lambda language-lawyer c++11 c++14

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

Android - 精确的数学计算

我遇到了一个问题,我正在开发一个应用程序,它应该能够进行一些数学计算。这些计算必须准确(或者不是明显错误)

但是这个简单的代码

double a = 3.048d;
double b = 1000d;

double c = a / b;
Run Code Online (Sandbox Code Playgroud)

给我一个错误的结果 c 不是预期的 0.003048,而是 0.00304800000000000004,这显然是错误的。

double d = 3.048 / 1000; 
Run Code Online (Sandbox Code Playgroud)

第二个代码片段给出了正确的结果。

我知道用计算机计算时所有浮点算法都不准确,但我不知道如何解决这个问题。

提前致谢!
路德维希

开发:
- Android 2.2
测试设备:
- HTC Desire

java android

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

标签 统计

android ×1

c++ ×1

c++11 ×1

c++14 ×1

java ×1

lambda ×1

language-lawyer ×1