小编Nat*_*ica的帖子

浮点数学是否破碎?

请考虑以下代码:

0.1 + 0.2 == 0.3  ->  false
Run Code Online (Sandbox Code Playgroud)
0.1 + 0.2         ->  0.30000000000000004
Run Code Online (Sandbox Code Playgroud)

为什么会出现这些不准确之处?

language-agnostic math floating-point floating-accuracy

2798
推荐指数
28
解决办法
28万
查看次数

为什么模板只能在头文件中实现?

引自C++标准库:教程和手册:

目前使用模板的唯一可移植方法是使用内联函数在头文件中实现它们.

为什么是这样?

(澄清:头文件不是唯一的便携式解决方案.但它们是最方便的便携式解决方案.)

c++ templates c++-faq

1660
推荐指数
14
解决办法
46万
查看次数

我可以使用哪种'clearfix'方法?

我有一个div包装两列布局的古老问题.我的侧边栏是浮动的,所以我的容器div无法包装内容和侧边栏.

<div id="container">
  <div id="content"></div>
  <div id="sidebar"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

似乎有很多方法可以修复Firefox中的明确错误:

  • <br clear="all"/>
  • overflow:auto
  • overflow:hidden

在我的情况下,唯一似乎正常工作的是<br clear="all"/>解决方案,这有点邋.. overflow:auto给我讨厌的滚动条,overflow:hidden肯定有副作用.此外,IE7显然不应该由于其不正确的行为而遭受这个问题,但在我的情况下,它与Firefox的痛苦相同.

目前我们可以采用哪种方法最强大?

css clearfix

856
推荐指数
17
解决办法
46万
查看次数

为什么这个if语句结合赋值和相等性检查返回true?

我一直在思考一些初学者的错误,最后我得到了if陈述中的错误。我对此扩展了一些代码:

int i = 0;
if (i = 1 && i == 0) {
    std::cout << i;
}
Run Code Online (Sandbox Code Playgroud)

我已经看到if语句返回真实的,它couti作为1。如果在if语句中i分配1了if,为什么i == 0返回true

c++ if-statement

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

const引用类成员是否延长了临时生命?

为什么这样:

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

class Sandbox
{
public:
    Sandbox(const string& n) : member(n) {}
    const string& member;
};

int main()
{
    Sandbox sandbox(string("four"));
    cout << "The answer is: " << sandbox.member << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

给出输出:

答案是:

代替:

答案是:四

c++ temporary const-reference ctor-initializer

157
推荐指数
3
解决办法
4万
查看次数

为什么在非const的私有时不调用公共const方法?

考虑以下代码:

struct A
{
    void foo() const
    {
        std::cout << "const" << std::endl;
    }

    private:

        void foo()
        {
            std::cout << "non - const" << std::endl;
        }
};

int main()
{
    A a;
    a.foo();
}
Run Code Online (Sandbox Code Playgroud)

编译器错误是:

错误:'void A :: foo()'是私有的.

但是当我删除私有它时它才起作用.为什么在非const的私有时不调用public const方法?

换句话说,为什么在访问控制之前会出现重载决策?这很奇怪.你认为它是一致的吗?我的代码工作,然后我添加一个方法,我的工作代码根本不编译.

c++ overloading member-functions overload-resolution

114
推荐指数
11
解决办法
7027
查看次数

"int&foo()"在C++中的含义是什么?

在阅读关于左值和左值的这个解释时,这些代码行向我伸出:

int& foo();
foo() = 42; // OK, foo() is an lvalue
Run Code Online (Sandbox Code Playgroud)

我在g ++中尝试过它,但编译器说"对foo()的未定义引用".如果我加

int foo()
{
  return 2;
}

int main()
{
  int& foo();
  foo() = 42;
}
Run Code Online (Sandbox Code Playgroud)

它编译很好,但运行它会产生分段错误.就行了

int& foo();
Run Code Online (Sandbox Code Playgroud)

它本身既编译又运行没有任何问题.

这段代码是什么意思?如何为函数调用赋值,为什么它不是rvalue?

c++ function return-by-reference

113
推荐指数
7
解决办法
2万
查看次数

如何通过需要自由函数的成员函数?

问题如下:考虑这段代码:

#include <iostream>


class aClass
{
public:
    void aTest(int a, int b)
    {
        printf("%d + %d = %d", a, b, a + b);
    }
};

void function1(void (*function)(int, int))
{
    function(1, 1);
}

void test(int a,int b)
{
    printf("%d - %d = %d", a , b , a - b);
}

int main (int argc, const char* argv[])
{
    aClass a();

    function1(&test);
    function1(&aClass::aTest); // <-- How should I point to a's aClass::test function?

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我如何使用a's aClass::test …

c++ arguments function-pointers parameter-passing pointer-to-member

106
推荐指数
6
解决办法
23万
查看次数

为什么初始化列表中的元素数会导致模糊的调用错误?

为什么doSomething编译器会先调用前两个,但是在列表中使用两个元素会导致调用不明确?

#include <vector>
#include <string>

void doSomething(const std::vector<std::string>& data) {}

void doSomething(const std::vector<int>& data) {}

int main(int argc, char *argv[])
{
    doSomething({"hello"}); // OK
    doSomething({"hello", "stack", "overflow"}); // OK
    doSomething({"hello", "stack"}); // C2668 'doSomething': ambiguous call

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ initializer-list ambiguous overload-resolution

59
推荐指数
3
解决办法
2169
查看次数

为什么`const T&`不确定是const?

template<typename T>
void f(T a, const T& b)
{
    ++a; // ok
    ++b; // also ok!
}

template<typename T>
void g(T n)
{
    f<T>(n, n);
}

int main()
{
    int n{};
    g<int&>(n);
}
Run Code Online (Sandbox Code Playgroud)

请注意:b是的const T&,++b没关系!

为什么const T&不确定是const?

c++ templates const const-reference function-templates

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