小编Leo*_*eon的帖子

将成员函数从基类移动到派生类会破坏程序,这是没有明显原因的

这个(化妆)问题最初是作为一个谜题制定的,隐藏了一些可能有助于更快地看到问题的细节.向下滚动以查看更简单的MCVE版本.


原创(a-la puzzle)版本

我有这段代码输出0:

#include <iostream>
#include <regex>

using namespace std;

regex sig_regex("[0-9]+");
bool oldmode = false;

template<class T>
struct B
{
    T bitset;

    explicit B(T flags) : bitset(flags) {}

    bool foo(T n, string s)
    {
        return bitset < 32                   // The mouth is not full of teeth
               && 63 > (~n & 255) == oldmode // Fooness holds
               && regex_match(s, sig_regex); // Signature matches
    }
};

template<class T>
struct D : B<T>
{
    D(T flags) …
Run Code Online (Sandbox Code Playgroud)

c++ templates namespaces c++11

51
推荐指数
3
解决办法
3304
查看次数

当重载函数作为参数参与时,模板参数推导如何工作?

这是一个更复杂的问题,如果参数是一个重载函数,重载决策是如何工作的?

下面的代码编译没有任何问题:

void foo() {}
void foo(int) {}
void foo(double) {}
void foo(int, double) {}

// Uncommenting below line break compilation
//template<class T> void foo(T) {}

template<class X, class Y> void bar(void (*f)(X, Y))
{
    f(X(), Y());
}

int main()
{
    bar(foo);
}
Run Code Online (Sandbox Code Playgroud)

模板参数推导似乎不是一项具有挑战性的任务 - 只有一个函数foo()接受两个参数.但是,取消注释模板重载foo()(仍然只有一个参数)会破坏编译,没有明显的原因.使用gcc 5.x/6.x和clang 3.9编译失败.

可以通过重载决策/模板参数推导的规则来解释它还是应该被认为是那些编译器中的缺陷?

c++ language-lawyer overload-resolution argument-deduction

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

为什么C++容器不允许不完整的类型?

为什么 C++不允许实例化不完整类型的容器?

编写没有这个限制的容器当然是可能的 - boost :: container完全能够做到这一点.据我所知,它似乎没有给出任何性能或其他类型的增益,但标准声明它是未定义的行为.

例如,它确实阻止了构建递归数据结构.

为什么C++标准会强加这种任意限制呢?尽可能允许不完整类型作为模板参数的缺点是什么?

c++ templates stl incomplete-type

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

我怎么知道STL对象需要多少内存?

我需要在程序中收集有关内存使用情况的统计信息.

我的代码主要使用STL编写.

有没有办法了解STL对象消耗了多少内存?

例如,

string s1 = "hello";
string s2 = "hellohellohellohellohellohellohellohellohellohellohellohellohello";
Run Code Online (Sandbox Code Playgroud)

消耗了多少内存s1s2?显然,sizeof(string)+s1.length()不太准确.

c++ memory stl

15
推荐指数
2
解决办法
2545
查看次数

std :: partial_sum和std :: inclusive_scan之间有什么区别?

在阅读有关std :: inclusive_scan的内容时,似乎没有任何示例.
它让我觉得非常类似于std :: partial_sum.

partial_sum:

template< class InputIt, class OutputIt >
OutputIt partial_sum( InputIt first,
                      InputIt last, OutputIt d_first );
Run Code Online (Sandbox Code Playgroud)

inclusive_scan:

template< class InputIt, class OutputIt >
OutputIt inclusive_scan( InputIt first,
                         InputIt last, OutputIt d_first );
Run Code Online (Sandbox Code Playgroud)

有人可以详细说明他们的分歧吗?我何时会选择一个而不是另一个?

c++ numeric stl-algorithm c++17

15
推荐指数
2
解决办法
1323
查看次数

"std :: cout << std :: endl;"怎么样?编译?

大多数IO流操纵器都是具有以下签名的常规函数​​:

std::ios_base& func( std::ios_base& str );
Run Code Online (Sandbox Code Playgroud)

但是,一些操纵器(包括最常用的操纵器 - std::endlstd::flush)是以下形式的模板:

template< class CharT, class Traits >
std::basic_ostream<CharT, Traits>& func(std::basic_ostream<CharT, Traits>& os);
Run Code Online (Sandbox Code Playgroud)

然后,如果std::cout << std::endl;以下示例失败,如何编译成功:

$ cat main.cpp 
#include <iostream>

int main()
{
    auto myendl = std::endl;
    std::cout << myendl;
}

$ g++ -std=c++11    main.cpp   -o main
main.cpp: In function ‘int main()’:
main.cpp:5:24: error: unable to deduce ‘auto’ from ‘std::endl’
     auto myendl = std::endl;
                        ^
Run Code Online (Sandbox Code Playgroud)

很明显,上下文(in std::cout << std::endl;)有助于编译器消除对引用的歧义std::endl.但是,管理该程序的规则是什么?对于重载分辨率来说,这似乎是一个真正的挑战,它必须同时回答两个问题:

  1. 哪个专业std::endl<CharT, Traits>() …

c++ templates overload-resolution iomanip

15
推荐指数
3
解决办法
1961
查看次数

什么是python的std :: lower_bound和std :: upper_bound C++算法的等价物?

python是否提供在排序列表上执行二进制搜索的功能,类似于C++标准库的算法std::lower_boundstd::upper_bound算法?

python binary-search

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

git rebase - 添加原始提交哈希以提交消息

有没有办法cherry-pick -x在rebase中做同样的事情(将原始提交的哈希添加到复制的提交的消息中)?

我现在可以通过更换以下内容来解决它

git checkout other-branch
git rebase master
git checkout master
git merge other-branch
Run Code Online (Sandbox Code Playgroud)

git checkout master
....
git cherry-pick -x other-branch^^^^
git cherry-pick -x other-branch^^^
git cherry-pick -x other-branch^^
git cherry-pick -x other-branch^
git cherry-pick -x other-branch
Run Code Online (Sandbox Code Playgroud)

git rebase

8
推荐指数
2
解决办法
447
查看次数

git的耐心差异算法的实现是否正确?

Stackoverflow上的这个问题似乎是耐心差异算法应用的一个很好的候选者.然而,在测试我的潜在答案时,我发现这git diff --patience不符合我的期望(在这种情况下,与默认的diff算法没有区别):

$ cat a
/**
 * Function foo description.
 */
function foo() {}

/**
 * Function bar description.
 */
function bar() {}

$ cat b
/**
 * Function bar description.
 */
function bar() {}

$ git diff --no-index --patience a b
diff --git a/a b/b
index 3064e15..a93bad0 100644
--- a/a
+++ b/b
@@ -1,9 +1,4 @@
 /**
- * Function foo description.
- */
-function foo() {}
-
-/**
  * Function bar description.
  */
 function …
Run Code Online (Sandbox Code Playgroud)

git git-diff

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

为什么g ++在带有转换运算符和不可访问函数调用运算符的类型中失败了std :: function <> init?

这段代码在g ++ 4.9及更高版本(包括从svn current构建时)失败,但是使用clang ++和microsofts编译器(来自VS2015)编译时没有警告.

#include <functional>

struct A {
  void operator()() const {}
};
struct B {
  void operator()() const {}
};
struct C : private A, private B
{
  operator std::function<void()>() const { return nullptr; }
};

int main()
{
  std::function<void()> f{C{}};
}
Run Code Online (Sandbox Code Playgroud)

建设f中的main()失败,因为operator()是在结构暧昧C.

为什么g ++认为这是不明确的?函数调用操作符C是私有继承的,不可访问.向void operator()() conststruct 添加私有或显式删除C会使代码编译并按预期使用转换运算符.为什么这些无法访问的运营商在无法访问的继承运营商时不会出现问题?

c++ gcc language-lawyer c++11

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