小编Gob*_*0st的帖子

为什么myClassObj ++++不会产生编译错误:'++'需要l-value就像buildin类型那样?

为什么myint ++++可以用VS2008编译器和gcc 3.42编译器编译好?我期待编译器说需要左值,例子见下文.

struct MyInt
{
    MyInt(int i):m_i(i){}

    MyInt& operator++() //return reference,  return a lvalue
    {
        m_i += 1;
        return *this;
    }

    //operator++ need it's operand to be a modifiable lvalue
    MyInt operator++(int)//return a copy,  return a rvalue
    {
        MyInt tem(*this);
        ++(*this);
        return tem;
    }

    int     m_i;
};

int main()
{
    //control: the buildin type int
    int i(0);
    ++++i;  //compile ok
    //i++++; //compile error :'++' needs l-value, this is expected

    //compare 
    MyInt  myint(1);
    ++++myint;//compile ok
    myint++++;//expecting compiler say need lvalue …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading lvalue

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

Gotw 67中的一个例子

http://www.gotw.ca/gotw/067.htm中有一个例子

int main()
{
  double x = 1e8;
  //float x = 1e8;
  while( x > 0 )
  {
    --x;
  }
}
Run Code Online (Sandbox Code Playgroud)

当你将double改为float时,它在VS2008中是一个无限循环.根据Gotw的解释:

如果float不能准确表示0到1e8之间的所有整数值,该怎么办?然后修改后的程序将开始倒计时,但最终将达到无法表示的值N和N-1 == N(由于浮点精度不足)......然后循环将保持卡住状态在该值上,直到运行程序的机器耗尽电量.

根据我的理解,IEEE754浮点数是单精度(32位),浮点数范围应为+/- 3.4e +/- 38,它应该有7位数字.

但我仍然不明白这究竟是怎么发生的:"最终达到一个无法表示的值N和N-1 == N(由于浮点精度不足)." 有人可以尝试解释这一点吗?

一些额外的信息:当我使用双x = 1e8时,它在大约1秒内完成,当我将其更改为浮动x = 1e8时,它运行的时间更长(5分钟后仍然运行),如果我将其更改为float x = 1e7;,它在大约1秒钟内完成.

我的测试环境是VS2008.

顺便说一句,我不是要求基本的IEEE 754格式解释,因为我已经明白了.

谢谢

c++ floating-point precision gotw

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

列表初始化编译错误

用于列表初始化的c ++ 11标准8.5.4示例说:

std::map<std::string,int> anim = { {"bear",4}, {"cassowary",2}, {"tiger",7} };
Run Code Online (Sandbox Code Playgroud)

但我已经尝试过VC10,gcc 4.6和Comeau,那些编译器都没有让这个通过?这是为什么 ?

c++ c++11

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

内存中的DLL大小和硬盘上的大小

内存中的DLL大小和硬盘上的大小之间是否存在关系?

这是因为我正在使用任务管理器扩展(MS),我可以转到列表中的EXE并右键单击 - >模块,然后我可以看到此EXE正在使用的所有DLL.它有一个Length列,但是以字节为单位?并且DLL的值(Length)似乎与硬盘上的(DLL)大小不同.为什么?

c++ dll optimization performance

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

为什么ADL优先于'std namespace'中的函数,但是等于用户定义的命名空间中的函数?

我有两个用于ADL的片段用于演示目的.这两个片段都是由VC10,gcc和comeau C++编译器编译的,结果对于这三个片段都是相同的.

<1> ADL反对使用用户定义的命名空间的指令:

#include <algorithm>
namespace N 
{ 
    struct T {}; 
    void swap(T,T) {} 
} 

namespace M 
{ 
    void swap(N::T,N::T) {} 
} 

int main() 
{ 
    using M::swap; 
    N::T o1,o2;
    swap(o1,o2); 
}
Run Code Online (Sandbox Code Playgroud)

编译结果:

error C2668: 'M::swap' : ambiguous call to overloaded function
could be 'void M::swap(N::T,N::T)'
or       'void N::swap(N::T,N::T)' [found using argument-dependent lookup]
Run Code Online (Sandbox Code Playgroud)

这是预期的,因为ADL不优先于正常查找结果加上ADL不是二等公民,ADL搜索结果与正常(非ADL)无法定义查找联合.这就是为什么我们有歧义.

<2> ADL反对使用std命名空间的指令:

#include <algorithm>
namespace N 
{ 
    struct T {}; 
    void swap(T,T) {}  //point 1
} 

namespace M 
{ 
    void swap(N::T,N::T) {} 
} 

int main() 
{ …
Run Code Online (Sandbox Code Playgroud)

c++ namespaces using-directives argument-dependent-lookup

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

生成随机 API 密钥,提供了 2 种方法,有什么区别吗?

我正在使用 java 7 生成一些随机 API 密钥(256 位长),下面提供了两种方法,generate() 和 generate2()。有什么区别吗?如果是这样,哪个更安全/更好?

提前致谢。

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.xml.bind.DatatypeConverter;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

public class RandomAESKeyGen {
    public static String generate(final int keyLen) throws NoSuchAlgorithmException {

        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        keyGen.init(keyLen);
        SecretKey secretKey = keyGen.generateKey();
        byte[] encoded = secretKey.getEncoded();
        return DatatypeConverter.printHexBinary(encoded).toLowerCase();
    }

    public static String generate2(final int keyLen) throws NoSuchAlgorithmException {

        SecureRandom random = new SecureRandom();
        byte bytes[] = new byte[keyLen/8];
        random.nextBytes(bytes);
        return DatatypeConverter.printHexBinary(bytes).toLowerCase();
    }

    public static void main(String[] args) {
        String key …
Run Code Online (Sandbox Code Playgroud)

java security random api-key java-7

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

除了与内存分配相关的东西之外,还是必须的

void*从内存分配相关的东西除了必要的C++?能给我举个例子?

c++ void-pointers

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

使用Apache Camel Exchange作为参数进行单元测试功能

我正在进行java驼峰开发,我希望单元测试(junit4)一系列函数与Exchange作为参数传入.

例如 :

public finalObject getProperty(final Exchange exchange, final String property) throws Exception {
   //all about getting property from xml message in exchange via xpath
}
Run Code Online (Sandbox Code Playgroud)

问题:1>我可以使用EasyMock来模拟Exchange吗?以及如何在交换机内设置预定义的xml作为传入消息?

2>如果不是,我需要设置骆驼测试吗?如何使用camel test在交换机内设置预定义的xml作为传入消息.

非常感谢.

unit-testing easymock apache-camel junit4

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

Java集合binarySearch无法正常工作

我只是想尝试使用原生Java二进制搜索,希望它总能找到第一次出现.但它并不总是第一次出现,我在这里做错了什么?

import java.util.*;

class BinarySearchWithComparator
{
  public static void main(String[] args)
  {
    // Please scroll down to see 'User' class implementation.
    List<User> l = new ArrayList<User>();
    l.add(new User(10, "A"));
    l.add(new User(10, "A"));
    l.add(new User(10, "A"));

    l.add(new User(20, "B"));
    l.add(new User(20, "B"));
    l.add(new User(20, "B"));
    l.add(new User(20, "B"));
    l.add(new User(20, "B"));
    l.add(new User(20, "B"));
    l.add(new User(20, "B"));


    l.add(new User(30, "C"));
    l.add(new User(30, "C"));
    l.add(new User(30, "C"));
    l.add(new User(30, "C"));

    Comparator<User> c = new Comparator<User>() {
      public int compare(User u1, User u2) …
Run Code Online (Sandbox Code Playgroud)

java collections binary-search

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

对于来自网络的大型 XML 消息,我应该使用哪种 postgres 数据类型?

我有传入的 xml,它可能与 5M 一样大,我需要将它与 postgres 9.1 一起存储。我应该使用哪种数据类型?

bytea 
character varying
text
Run Code Online (Sandbox Code Playgroud)

或者是其他东西 ?

顺便说一句,xml 本身包含一些 base64 格式的二进制数据,这在 postgres 中选择数据类型时有什么区别吗?

谢谢

xml database postgresql

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