我有一个可变参数函数,我想在第一个参数类型上重载.
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) 我不明白为什么行为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) 我需要构建简单的服务器
该计划的一些重要方面:
基本上我的问题是:
我应该用什么编程语言呢?
Java?
C++?
我们有:
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[]"?
您可以使用模板查找数组的长度.
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]意思?
如何在仍然使用模板获取其大小的同时允许访问数组的地址?
是否有传递构造函数参数的首选实践?特别是如果这些构造函数参数用于初始化成员变量.
一个简化的例子.
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,但我不确定我理解所有的优点和缺点.
我在两个非常相关的类的实现中使用私有继承.这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) 我想实现一个Go应用程序,它将一些私有数据发送到服务器.服务器上的代码是PHP并使用"基于MD5的分组密码".PHP加密/解密代码在这里:http: //www.jonasjohn.de/snippets/php/md5-based-block-cipher.htm
Go中是否有相同的例程?
c++ ×6
c++11 ×2
go ×2
arrays ×1
constructor ×1
cstring ×1
encryption ×1
go-templates ×1
java ×1
md5 ×1
optimization ×1
php ×1
stdstring ×1
templates ×1