小编Wil*_*mKF的帖子

C++语言模板问题

下面是一个小测试用例,演示了我试图使用C++中的模板解决的问题:

template<typename T>
void
unused(T const &) {
  /* Do nothing. */
}

int main() {
  volatile bool x = false;
  unused(!x); // type of "!x" is bool
}
Run Code Online (Sandbox Code Playgroud)

如下所示,g ++ v3.4.6编译器抱怨:

test.cc: In constructor `test::test()':
test.cc:11: error: invalid initialization of reference of type 'const volatile bool&' from expression of type 'volatile bool'
test.cc:3: error: in passing argument 1 of `void unused(const T&) [with T = volatile bool]'
Run Code Online (Sandbox Code Playgroud)

这里的目标是在优化的代码中使用未使用的抑制未使用的变量警告.我有一个执行断言检查的宏,在优化代码中断言消失了,但是我希望断言表达式中的任何变量都保持引用,这样我才能在优化代码中得到未使用的变量警告.在unused()模板函数的定义中,我使用引用,以便不会无意中运行复制构造函数代码,以便编译器可以完全忽略对未使用的调用.

对于那些感兴趣的人,断言宏看起来像这样:

#ifdef NDEBUG
#  define Assert(expression) unused(expression)
#else // not NDEBUG …
Run Code Online (Sandbox Code Playgroud)

c++ templates

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

什么是s2k算法?

s2k算法的定义是什么?例如,"PBKDF2(SHA-1)"是s2k算法.

这是一些引用s2k的Botan代码:

  AutoSeeded_RNG rng;

  std::auto_ptr<S2K> s2k(get_s2k("PBKDF2(SHA-1)"));
  s2k->set_iterations(8192);
  s2k->new_random_salt(rng, 8);

  SymmetricKey bc_key = s2k->derive_key(key_len, "BLK" + passphrase);
  InitializationVector iv = s2k->derive_key(iv_len, "IVL" + passphrase);
  SymmetricKey mac_key = s2k->derive_key(16, "MAC" + passphrase);
Run Code Online (Sandbox Code Playgroud)

encryption algorithm botan s2k

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

为什么connect()会在端口上给FreeBSD提供间歇性的EINVAL?

我在我的C++应用程序中出现了从32位Linux移植到32位FreeBSD 8.1时出现的故障.我有一个无法连接的TCP套接字连接.在对connect()的调用中,我得到了errno == EINVAL的错误结果,connect()的手册页没有覆盖.

这个错误意味着什么,哪个参数无效?消息只是说:"无效的参数".

以下是连接的一些细节:

family: AF_INET
len: 16
port: 2357
addr: 10.34.49.13
Run Code Online (Sandbox Code Playgroud)

但它并不总是失败.只有让机器空闲几个小时后,FreeBSD版本才会失败.但是在失败一次之后,它会可靠地工作,直到你让它再次闲置一段时间.

以下是一些代码:

void setSocketOptions(const int skt);
void buildAddr(sockaddr_in &addr, const std::string &ip,
               const ushort port);
void deepBind(const int skt, const sockaddr_in &addr);


void
test(const std::string &localHost, const std::string &remoteHost,
     const ushort localPort, const ushort remotePort,
     sockaddr_in &localTCPAddr, sockaddr_in &remoteTCPAddr)
{
  const int skt = socket(AF_INET, SOCK_STREAM, 0);

  if (0 > skt) {
    clog << "Failed to create socket: (errno " << errno
         << ") " << …
Run Code Online (Sandbox Code Playgroud)

sockets porting freebsd tcp connect

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

通过调用名称在Windows中调用Ghostscript?

我应该怎么做才能通过其调用名称在Windows中调用Ghostscript?我将Ghostscript bin文件夹添加到Windows PATH和Path变量,但它不起作用,'gswin32c.exe'和'gswin32c'都没有.注销然后重新登录也没有用.我该如何解决这个问题?也许我使用了错误的调用名称?

windows command-line path ghostscript

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

如何在Visual Studio 2008中撤消"从项目中排除"

可能重复:
撤消VS'从项目中排除'?

我错误地选择了"从项目中排除"菜单项,以为我可以选择要排除的内容,但它排除了当前打开的文件.

我没有注意到那是什么文件,并且没有启用撤消.

如何撤消此操作?如何检测排除的文件?有某种日志文件吗?

undo visual-studio-2008 visual-studio

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

朋友见基类吗?

给出示例代码:

class Base {
public:
  bool pub;
protected:
  bool prot;
};

class Derived : private Base {
  friend class MyFriend;
};

class MyFriend {
  Derived _derived;

  void test() {
    // Does standard provide me access to _derived.pub and _derived.prot?
    cout << "Am I allowed access to this: " << _derived.pub
         << " and this: " << _derived.prot;
  }
};
Run Code Online (Sandbox Code Playgroud)

做朋友会给我所有的访问权限,我会得到好像我是班级中的一员,我是他的朋友吗?换句话说,由于我是朋友,我可以获得私人继承的基类的受保护和公共成员吗?

c++ inheritance private protected friend

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

自我的typedef会有什么影响吗?

我遇到了一些具有以下内容的C++代码:

typedef Request Request;
Run Code Online (Sandbox Code Playgroud)

这只是一个无操作或者这种typedef实际有效果,如果有,它有什么影响?

c++ typedef self no-op

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

有没有更有效的方法从流中设置std :: vector?

目前,我设置的值std::vector<char>std::ostringstream如下:

void
foo(std::vector<char> &data, std::stringstream &stream) {
  data = std::vector<char>(stream.str().begin(), stream.str().end());
}
Run Code Online (Sandbox Code Playgroud)

我想知道是否有更有效的方法在C++中使用STL或者我在这里给出的方法是否合适?std::stringstream相反,我会更好吗?

c++ stl stringstream ostringstream stdvector

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

我可以依赖新的bool初始化为false吗?

在C++中,我可以依赖于在所有情况下初始化为false的新bool吗?

bool *myBool = new bool();

assert(false == *myBool);  // Always the case in a proper C++ implementation?
Run Code Online (Sandbox Code Playgroud)

(更新了反映评论的代码.)

c++ portability boolean default-constructor language-lawyer

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

Python列表是单链接还是双链接列表?

我想知道使用append()构建Python v2.7列表的复杂程度是多少?Python列表是双重链接的,因此它是恒定的复杂性还是单独链接,因此是线性复杂性?如果它是单链接的,我怎样才能在线性时间内从迭代中建立一个列表,该列表按照从头到尾的顺序提供列表的值?

例如:

def holes_between(intervals):
  # Compute the holes between the intervals, for example:
  #     given the table: ([ 8,  9] [14, 18] [19, 20] [23, 32] [34, 49])
  #   compute the holes: ([10, 13] [21, 22] [33, 33])
  prec = intervals[0][1] + 1 # Bootstrap the iteration
  holes = []
  for low, high in intervals[1:]:
    if prec <= low - 1:
      holes.append((prec, low - 1))
    prec = high + 1
  return holes
Run Code Online (Sandbox Code Playgroud)

python complexity-theory list singly-linked-list doubly-linked-list

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