小编def*_*ode的帖子

在可变参数模板函数中对ostream进行重载

我有一个可变参数函数,我想在第一个参数类型上重载.

void write( void ) { }

void write( std::ostream& ) { }

template< typename Head, typename... Rest >
void write( std::ostream& out, Head&& head, Rest&&... rest )
{
   out << head;
   write( out, std::forward<Rest>(rest)... );
}

template< typename... Args >
void write( Args&&... args )
{
   write( std::cout, std::forward<Args>(args)... );
}
Run Code Online (Sandbox Code Playgroud)

但是这些功能并不像预期的那样.

write( "## to cout ##" ); // printed to stdout as expected
write( std::cerr, "## to cerr ##" ); // printed to stderr as expected
std::ostringstream oss; …
Run Code Online (Sandbox Code Playgroud)

c++ overload-resolution variadic-templates c++11

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

ParseFiles函数在html/template中的不同行为

我不明白为什么行为func (t *Template) Parsefiles(...不同func ParseFiles(....这两个函数都来自"html/template"包.

package example

import (
    "html/template"
    "io/ioutil"
    "testing"
)

func MakeTemplate1(path string) *template.Template {
    return template.Must(template.ParseFiles(path))
}

func MakeTemplate2(path string) *template.Template {
    return template.Must(template.New("test").ParseFiles(path))
}

func TestExecute1(t *testing.T) {
    tmpl := MakeTemplate1("template.html")

    err := tmpl.Execute(ioutil.Discard, "content")
    if err != nil {
        t.Error(err)
    }
}

func TestExecute2(t *testing.T) {
    tmpl := MakeTemplate2("template.html")

    err := tmpl.Execute(ioutil.Discard, "content")
    if err != nil {
        t.Error(err)
    }
}
Run Code Online (Sandbox Code Playgroud)

这退出时出现错误:

--- FAIL: TestExecute2 (0.00 seconds)
    parse_test.go:34: html/template:test: "test" …
Run Code Online (Sandbox Code Playgroud)

go go-templates

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

选择哪种技术用于大规模IO操作服务器

我需要构建简单的服务器

  1. 读取(可能很大)的xml文件
  2. 在内存中处理它们(例如将它们转换为不同的xml结构)
  3. 将它们写回磁盘.

该计划的一些重要方面:

  • 速度
  • 分发服务器的能力.这意味着放置(这意味着什么)几个这样的服务器,每个服务器将处理不同数量的xml文件.
  • 跨平台
  • 建在一个非常紧张的死线

基本上我的问题是:
我应该用什么编程语言呢?

Java?

  • 发展速度
  • 跨平台
  • 使用正确的配置进行IO操作很高(在此处添加Web链接).

C++?

  • 执行速度
  • 跨平台(使用正确的库).
  • 但发展速度较慢.

c++ java optimization cross-platform

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

将STL字符串数组转换为const char*数组的最有效方法是什么?

我们有:

 std::string string_array[2];

 string_array[0] = "some data";

 string_array[1] = "some more data";

 char* cstring_array[2];
Run Code Online (Sandbox Code Playgroud)

将数据从string_array复制到cstring_array的最有效方法是什么?或者将string_array传递给函数,需要" const char* cstring_array[]"?

c++ arrays cstring stdstring

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

使用模板获取数组的大小和结束地址

您可以使用模板查找数组的长度.

template<typename T, size_t N>
size_t arraylen( T(&)[N] )
{ return N; }
Run Code Online (Sandbox Code Playgroud)

我想把这个想法更进一步.

struct Foo
{
   template< typename T, size_t N >
   Foo( /* ??? */ ) : ptr(?), size(?) { }

   char* ptr;
   size_t size;
};

int main()
{
   Foo foo("test");

   const char bar[] = "test2";
   Foo foo2(bar);

   const char* baz = bar;
   Foo foo3(baz); // compiler error.
}
Run Code Online (Sandbox Code Playgroud)

但是,对于我的生活,我无法获得编译的语法.我认为我缺少的一部分是我真的不明白这T(&)[N]意味着什么.

什么T(&)[N]意思?

如何在仍然使用模板获取其大小的同时允许访问数组的地址?

c++ templates metaprogramming

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

构造函数的首选参数传递

是否有传递构造函数参数的首选实践?特别是如果这些构造函数参数用于初始化成员变量.

一个简化的例子.

class Example
{
public:
   Example( /*type-1*/ str, /*type-2*/ v ):
      m_str( str ),
      m_v( v )
   { }

   /* other methods */

private:
   std::string m_str;
   std::complex<float> m_v;
};
Run Code Online (Sandbox Code Playgroud)

选项是:

  • 按值传递,然后std::move将对象传入成员.
  • const&,然后将参数复制到成员中.
  • &&,然后使用参数初始化成员.

什么应该是我的默认/首选参数传递样式?
它是否随着不同的参数类型而改变?

我的直觉说使用rvalue-references,但我不确定我理解所有的优点和缺点.

c++ constructor rvalue-reference c++11

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

私有继承和交换

我在两个非常相关的类的实现中使用私有继承.这using Base::X;是非常有用和优雅的.但是,我似乎找不到重用基类交换功能的优雅解决方案.

class A
{
public:
   iterator       begin();
   const_iterator begin() const;
   const_iterator cbegin() const;

   A clone();

   void swap( A& other );
};

class Const_A : private A
{
public:
   // I think using A::A; will be valid in C++0x
   Const_A( const A& copy) : A(copy) { }

   // very elegant, concise, meaningful
   using A::cbegin;

   // I'd love to write using A::begin;, but I only want the const overload
   // this is just forwarding to the const overload, still …
Run Code Online (Sandbox Code Playgroud)

c++ private-inheritance

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

是否有"基于MD5的分组密码"的Go版本?

我想实现一个Go应用程序,它将一些私有数据发送到服务器.服务器上的代码是PHP并使用"基于MD5的分组密码".PHP加密/解密代码在这里:http: //www.jonasjohn.de/snippets/php/md5-based-block-cipher.htm

Go中是否有相同的例程?

php encryption md5 go

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