小编rus*_*tyx的帖子

try-with-resources中的catch是否涵盖了括号中的代码?

它是由不明的文件,如果catch下一个try-with-resources覆盖初始化部分与否.

换句话说,给定此代码片段:

    try (InputStream in = getSomeStream()) {
        System.out.println(in.read());
    } catch (IOException e) {
        System.err.println("IOException: " + e.getMessage());
    }
Run Code Online (Sandbox Code Playgroud)

catch如果IOException被扔到里面我会被调用吗?getSomeStream()

或者catch仅覆盖花括号内的块,即 System.out.println(in.read())?

java try-with-resources

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

为什么GAE DataStore不支持对不同属性的多个不等式过滤器?

我知道这是DataStore的限制,但我只想弄清楚原因.我读过bigtable文章,我找不到对不同列上的不等式过滤器的任何限制.它可以支持前缀和范围扫描.恕我直言,DataStore可以通过这两个操作支持多个不等式过滤器.你知道从DataStore获取功能的任何原因吗?

google-app-engine google-cloud-datastore

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

如何限制模板函子返回和参数类型

我的代码看起来像这样:

template<typename F>
void printHello(F f)
{
    f("Hello!");
}

int main() {
    std::string buf;
    printHello([&buf](const char*msg) { buf += msg; });
    printHello([&buf]() { });
}
Run Code Online (Sandbox Code Playgroud)

问题是 - 如何限制F类型只接受具有签名的lambda void(const char*),以便第二次调用printHello不会在内部某个不明显的位置失败,printHello而是在调用printHello错误的行上失败?

== ==编辑

我知道是std::function在这种特殊情况下(是什么,如果我真的想打印"你好"我会使用),解决它.但std::function实际上是其他的东西并且需要付出代价(无论成本如何都很小,截至2016年4月,GCC和MSVC 无法优化虚拟呼叫).所以我的问题可以看作是纯粹的学术问题 - 是否有"模板"方法来解决它?

c++ templates c++11

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

如何结合两个boost :: filesystem :: path?

我需要将绝对路径A与路径B结合起来,因为B既可以是相对的也可以是绝对的,最好使用boost :: filesystem。

换句话说,我想拥有:

  • /usr/home/+ abc=/usr/home/abc
  • /usr/home/+ ../abc= /usr/home/../abc(或者甚至更好/usr/abc- 这不是我的问题)
  • /usr/home/+ /abc=/abc

前两个对于/操作员来说很容易,但是我无法使第三个工作。

我试过了:

std::cout << boost::filesystem::path("/usr/home/") / "/abc";
Run Code Online (Sandbox Code Playgroud)

印刷品/usr/home//abc。

std::cout << boost::filesystem::path("/usr/home/") + "/abc";
Run Code Online (Sandbox Code Playgroud)

仍然打印/usr/home//abc。

当然,通过查看路径B并使用它,我可以“看到”路径B是绝对路径,但我不想硬编码前导的检查,/因为在Windows上路径可以不同(例如C:\\或\\)。

c++ boost boost-filesystem

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

OpenSSL 调试 - 如何在 openssl 中转储中间 ASN.1?

我有一个 PKCS#12 测试文件,其中有一个用 PBES2 (PBEWithHmacSHA256AndAES_256) 加密的单个条目,它在 OpenSSL 中不起作用(但在其他地方有效)。

所以我想弄清楚我的文件是否已损坏,或者 OpenSSL 是否无法正确处理 PBES2。

该文件被附加:test.p12(pass:test)

openssl pkcs12(v.1.0.2p-dev)的输出是:

$ openssl pkcs12 -info -nodes -in out.p12 -passin pass:test
MAC Iteration 100000
MAC verified OK
PKCS7 Data
Shrouded Keybag: PBES2<unsupported parameters>
Bag Attributes
    friendlyName: test
    localKeyID: 54 69 6D 65 20 31 35 33 30 38 32 31 38 34 39 32 39 39
Error outputting keys and certificates
10564:error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag:.\crypto\asn1\tasn_dec.c:1220:
10564:error:0D07803A:asn1 encoding routines:ASN1_ITEM_EX_D2I:nested asn1 error:.\crypto\asn1\tasn_dec.c:386:Type=X509_ALGOR
10564:error:0D08303A:asn1 encoding …
Run Code Online (Sandbox Code Playgroud)

c encryption openssl pkcs#12 openssl-engine

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

使用遗留代码(使用reinterpret_cast)真的是一种很好的技术吗?

下面的代码来自于一个关于C++的面试问题后这里.我从来不知道这种技术:)(虽然它声称是一个很好的:)).我的问题是:我们需要在哪种情况下使用它?您是否经常在真实的生产/遗留代码中看到它?

题:

为任何给定的Something*对象实现一个获取topSecretValue的方法.该方法应该是跨平台兼容的,不依赖于sizeof(int,bool,string).

class Something {
    Something() {
        topSecretValue = 42;
    }
    bool somePublicBool;
    int somePublicInt;
    std::string somePublicString;
private:
    int topSecretValue;
};
Run Code Online (Sandbox Code Playgroud)

回答:

创建另一个类,它具有相同顺序的Something的所有成员,但具有返回值的其他公共方法.您的副本类似于以下内容:

class SomethingReplica {
public:
    int getTopSecretValue() { return topSecretValue; } // <-- new member function
    bool somePublicBool;
    int somePublicInt;
    std::string somePublicString;
private:
    int topSecretValue;
};

int main(int argc, const char * argv[]) {
    Something a;
    SomethingReplica* b = reinterpret_cast<SomethingReplica*>(&a);
    std::cout << b->getTopSecretValue();
}
Run Code Online (Sandbox Code Playgroud)

在最终产品中避免这样的代码很重要,但在处理遗留代码时它仍然是一种很好的技术,因为它可以用来从库类中提取中间计算值.(注意:如果事实证明外部库的对齐与您的代码不匹配,您可以使用#pragma pack解决此问题.)

c++

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

线性搜索与二进制搜索效率

我目前正在研究不同的搜索算法,并且做了一些程序来看看效率上的差异。二进制搜索应该比线性搜索更快,但是时间的确显示了其他情况。我在代码中犯了一些错误还是这是一种特殊情况?

#include <chrono>
#include <unistd.h>

using namespace std;

const int n=1001;
int a[n];

void assign() {
    for (int i=0; i<n; i++) {
        a[i]=i;
    }
}

void print() {
    for (int i=0; i<n; i++) {
        cout << a[i] << endl;
    }
}

bool find1 (int x) {
    for (int i=0; i<n; i++) {
        if (x==a[i]){
            return true;
        } 
    } return false;
}

bool binsearch(int x) {
    int l=0,m; 
    int r=n-1;
    while (l<=r) {
        m = ((l+r)/2);
        if (a[m]==x) return true;
        if …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm performance search binary-search

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

了解虚拟派生类的大小

#include <iostream>
using namespace std;
class A {
  int a;
};
class B1 : virtual public A {
  int b1;
};
class B2 : virtual public A {
  int b2;
};
class C : public B1, public B2 {
  int c;
};

int main() {
  A obj1; B1 obj2; B2 obj3; C obj4;
  cout << sizeof(obj1) << endl;
  cout << sizeof(obj2) << endl;
  cout << sizeof(obj3) << endl;
  cout << sizeof(obj4) << endl;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

4
16
16 …
Run Code Online (Sandbox Code Playgroud)

c++ multiple-inheritance virtual-inheritance memory-layout

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

“perf annotate”内存加载/存储时间报告不一致

我很难解释英特尔性能事件报告。

\n

考虑以下主要读/写内存的简单程序:

\n
#include <stdint.h>\n#include <stdio.h>\n\nvolatile uint32_t a;\nvolatile uint32_t b;\n\nint main() {\n  printf("&a=%p\\n&b=%p\\n", &a, &b);\n  for(size_t i = 0; i < 1000000000LL; i++) {\n    a ^= (uint32_t) i;\n    b += (uint32_t) i;\n    b ^= a;\n  }\n  return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我用gcc -O2以下命令编译它并运行perf:

\n
#include <stdint.h>\n#include <stdio.h>\n\nvolatile uint32_t a;\nvolatile uint32_t b;\n\nint main() {\n  printf("&a=%p\\n&b=%p\\n", &a, &b);\n  for(size_t i = 0; i < 1000000000LL; i++) {\n    a ^= (uint32_t) i;\n    b += (uint32_t) i;\n    b ^= a;\n  }\n …
Run Code Online (Sandbox Code Playgroud)

performance assembly x86-64 micro-optimization perf

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

realloc引起的分段错误?

嘿,我正试图解决这个学校的运动..

编写一个程序,继续读取字符串并连接它们(将它们添加到单个字符串).串联应该在一个函数中发生,如果成功则返回1,如果失败则返回0.对于内存分配,只使用realloc!

调试程序时我没有收到任何错误,但是当我尝试运行程序时,在插入字符串之后,唯一出现的是"Segmentation Fault",它会是什么?这是代码:

#include<stdlib.h>
#include<stdio.h>
#include<string.h>

int cat(char **, char *);

int main(void)
{
  char string[51];
  char *output=NULL;
  char choice;
  do
  {
    printf("Please enter a string [<50 chars]: ");
    fgets(string,50,stdin);
    if(string[strlen(string)-1]=='\n') /* if newline was read as well */
      string[strlen(string)-1]=0;      /* discard it */
    if(cat(&output,string))
      printf("\n\nThe string now contains:\n%s\n",output);
    else
    {
      printf("error: memory (re-)allocation failed!\n\n");
      return 1; /* exit with error */ 
    }
    printf("Continue? (y/n) - ");
    fgets(string,3,stdin); /* read input from keyboard - leave a safety buffer to …
Run Code Online (Sandbox Code Playgroud)

c realloc dma

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