小编Man*_*bes的帖子

何时可以在初始化列表中省略外括号?

编译下面的代码时,我在VC2010中遇到错误C2078.

struct A
  {
  int foo;
  double bar;
  };

std::array<A, 2> a1 = 
  // error C2078: too many initializers
  {
    {0, 0.1},
    {2, 3.4}
  };

// OK
std::array<double, 2> a2 = {0.1, 2.3};
Run Code Online (Sandbox Code Playgroud)

我发现正确的语法a1

std::array<A, 2> a1 = 
  {{
    {0, 0.1},
    {2, 3.4}
  }};
Run Code Online (Sandbox Code Playgroud)

问题是:为什么需要额外的括号a1但不是必需的a2

更新

这个问题似乎并不特定于std :: array.一些例子:

struct B
  {
  int foo[2];
  };

// OK
B meow1 = {1,2};
B bark1 = {{1,2}};

struct C
  {
  struct 
    { 
    int a, b; 
    } …
Run Code Online (Sandbox Code Playgroud)

c++ visual-c++-2010 c++11

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

SLF4J使用varargs方法参数化日志记录

我必须是愚蠢的东西,但我似乎无法使用SLF4J 的varargs利用参数化日志记录方法.一个例子:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LoggingTest {

    @Test
    public void loggingTest() {
        Logger logger = LoggerFactory.getLogger(this.getClass());
        int x = 0xdeadbeef;
        long y = 0xdeadbeef;

        try {
            throw new Exception("This is a mighty exception!");
        } catch(Exception e) {
            logger.error("I wanna log {} and {} and {} with backtrace", x, y, 3, e);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在日志记录方法上,eclipse产生了这样一个警告:

The method error(String, Object, Object) in the type Logger is not applicable for the arguments (String, int, long, int, Exception) …
Run Code Online (Sandbox Code Playgroud)

java slf4j

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

保证向Kafka集群传递多条消息

如果我连续几次向Kafka集群发布消息(使用新的Producer API),我会Future从生产者那里获得每条消息的消息.

现在,假设我已经配置我的制片人有max.in.flight.requests.per.connection = 1retries > 0我只能等待最后的未来,可以肯定,所有先前也已交付(和顺序)?或者我需要等待所有期货?在代码中,我可以这样做:

Producer<String, String> producer = new KafkaProducer<>(myConfig);
Future<?> f = null;
for(MessageType message : messages){
  f = producer.send(new ProducerRecord<String,String>("myTopic", message.getKey(), message.getValue());
}
try {
  f.get();
} catch(ExecutionException e) {
  //handle exception
}
Run Code Online (Sandbox Code Playgroud)

而不是这个:

Producer<String, String> producer = new KafkaProducer<>(myConfig);
List<Future<?>> futureList = new ArrayList<>();
for(MessageType message : messages){
  futureList.add(producer.send(new ProducerRecord<String,String>("myTopic", message.getKey(), message.getValue()));
}
try {
  for(Future<?> f : futureList) {
    f.get();
  }
} catch(ExecutionException …
Run Code Online (Sandbox Code Playgroud)

java apache-kafka kafka-producer-api

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

可以使用从源文件读取的值来分配makefile变量吗?

假设有一个C程序,它将其版本存储char*在main.c 中的全局中.构建系统(gnu make)可以在构建时以某种方式提取此变量的值,以便构建的可执行文件可以具有程序中显示的确切版本名称吗?

我想要实现的是,鉴于来源:

char g_version[] = "Superprogram 1.32 build 1142";
Run Code Online (Sandbox Code Playgroud)

构建系统将生成一个名为的可执行文件 Superprogram 1.32 build 1142.exe

c makefile

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

使用RabbitMQ可以放慢同步发布/消耗速度

我正在评估RabbitMQ,虽然(AMQP本身,以及RabbitMQ)的总体印象是积极的,但结果并没有让我印象深刻.

我正在尝试同时发布和使用消息,并且已经实现了非常差的消息速率.我有一个持久的直接交换,它绑定到一个持久的队列,我向该交换发布持久性消息.消息正文的平均大小约为1000个字节.

我的发布大致如下:

AMQP.BasicProperties.Builder bldr = new AMQP.BasicProperties.Builder();
ConnectionFactory factory = new ConnectionFactory();
factory.setUsername("guest");
factory.setPassword("guest");
factory.setVirtualHost("/");
factory.setHost("my-host");
factory.setPort(5672);
Connection conn = null;
Channel channel = null;
ObjectMapper mapper = new ObjectMapper(); //com.fasterxml.jackson.databind.ObjectMapper
try {
    conn = factory.newConnection();
channel = conn.createChannel();
    channel.confirmSelect();
} catch (IOException e) {}

for(Message m : messageList) { //the size of messageList happens to be 9945
    try {
        channel.basicPublish("exchange", "", bldr.deliveryMode(2).contentType("application/json").build(), mapper.writeValueAsBytes(cm));
    } catch (Exception e) {}
}
try {
    channel.waitForConfirms();
    channel.close();
conn.close();
} catch (Exception e1) …
Run Code Online (Sandbox Code Playgroud)

amqp rabbitmq

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

子进程模块无法运行命令

我正在尝试在一组文件上执行Google的cpplint.py并将结果收集到一个日志文件中.但是,我还没有成功击败子进程模块.我目前的代码在这里:

import os, subprocess

rootdir = "C:/users/me/Documents/dev/"
srcdir = "project/src/"

with open(rootdir+srcdir+"log.txt", mode='w', encoding='utf-8') as logfile:
    for subdir, dirs, files in os.walk(rootdir+srcdir):
        for file in files:
            if file.endswith(".h") or file.endswith(".cpp"):
                filewithpath=os.path.join(subdir, file)
                cmd=['c:/Python27/python.exe','C:/users/me/Documents/dev/cpplint.py','--filter=-whitespace,-legal,-build/include,-build/header_guard/', filewithpath]               
                output = subprocess.check_output(cmd)
                logfile.write(output.decode('ascii'))
Run Code Online (Sandbox Code Playgroud)

尝试运行上面的代码会引发错误:

  File "C:\Python32\lib\site.py", line 159
    file=sys.stderr)
        ^ SyntaxError: invalid syntax Traceback (most recent call last):   File "C:\Users\me\Documents\dev\project\src\verifier.py", line 19, in <module>
    output = subprocess.check_output(cmd)   File "C:\Python32\lib\subprocess.py", line 511, in check_output
    raise CalledProcessError(retcode, cmd, output=output) subprocess.CalledProcessError: Command '['c:/Python27/python.exe', 'C:/users/me/Documents/dev/cpplint.py', '--filter=-whitespace,-legal,-build/include,-build/header_guard/', 'C:/users/me/Documents/dev/project/src/aboutdialog.cpp']' returned non-zero …
Run Code Online (Sandbox Code Playgroud)

python subprocess python-3.x

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

Google地图标记不会收到点击事件

我正在尝试使用QtWebkit和Google Maps.我遇到的问题是从QWebView调用C++插槽.QWebView中显示的页面是经过修改的Google Maps示例:

<!DOCTYPE html>
<html>
<head>
<title>Google Maps JavaScript API v3 Example: Event Simple</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="UTF-8">
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map_canvas { height: 100% }
</style>
<script type="text/javascript">
  function initialize() {
    var myOptions = {
      zoom: 4,
      center: new google.maps.LatLng(-25.363882, 131.044922),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    var map = new google.maps.Map(document.getElementById('map_canvas'),
        myOptions);

    var marker = new google.maps.Marker({
      position: map.getCenter(),
      map: map,
      title: …
Run Code Online (Sandbox Code Playgroud)

javascript qt google-maps qtwebkit

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

函数atof()将其结果舍入为整数部分

为什么圆形"14718.5084"到14718.5?有没有办法防止这种情况(即得到整数14718.5084)?

码:

double latitude=atof("14718.5084");
std::cout <<"latitude test "<<latitude<< "\n";
Run Code Online (Sandbox Code Playgroud)

输出是:

latitude test 14718.5
Run Code Online (Sandbox Code Playgroud)

谢谢

c++ atof

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