小编Dan*_* K.的帖子

C++ 11 variadic std :: function参数

一个名为teststd :: function <>的函数作为参数.

template<typename R, typename ...A>
void test(std::function<R(A...)> f)
{
    // ...
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我执行以下操作:

void foo(int n) { /* ... */ }

// ...

test(foo);
Run Code Online (Sandbox Code Playgroud)

编译器(gcc 4.6.1)说no matching function for call to test(void (&)(int)).

要使最后一行test(foo)编译并正常工作,我该如何修改该test()函数?在test()函数中,我需要f类型为std :: function <>.

我的意思是,是否有任何模板技巧让编译器确定函数的签名(foo在示例中),并将其std::function<void(int)>自动转换?

编辑

我想为lambdas(无论是声明的还是无状态的)做这项工作.

c++ templates variadic-templates c++11

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

调用模板基类的模板函数

可能重复:
我必须在何处以及为何要使用"template"和"typename"关键字?

这是代码:

template<typename T>
class base
{
public:
    virtual ~base();

    template<typename F>
    void foo()
    {
        std::cout << "base::foo<F>()" << std::endl;
    }
};

template<typename T>
class derived : public base<T>
{
public:
    void bar()
    {
        this->foo<int>(); // Compile error
    } 
};
Run Code Online (Sandbox Code Playgroud)

而且,在运行时:

derived<bool> d;
d.bar();
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

error: expected primary-expression before ‘int’
error: expected ‘;’ before ‘int’
Run Code Online (Sandbox Code Playgroud)

我知道非依赖名称和两阶段查找.但是,当函数本身是模板函数(foo<>()我的代码中的函数)时,我尝试了所有变通方法只是失败了.

c++ templates dependent-name

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

通过在Windows7 64bit上复制'lib'文件夹来安装PyCrypto

我正在尝试在我的本地计算机(Windows 7 64位)上安装PyCrypto 2.4.1.但是,我在'python setup.py install'上收到了以下消息.

运行安装

运行构建

运行build_py

运行build_ext

警告:未找到GMP或MPIR库; 没有构建Crypto.PublicKey._fastmath.

构建'Crypto.Random.OSRNG.winrandom'扩展

错误:无法找到vcvarsall.bat

我想问的是:我可以将lib包含Crypto文件夹的文件夹复制到我的应用程序所在的位置吗?我正在使用Python27运行时开发一个Google AppEngine应用程序,而且我只需要PyCrypto的本地库.

python google-app-engine pycrypto

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

在制作std :: string副本时的分配

您认为哪种实施更好?

std::string ToUpper( const std::string& source )
{
    std::string result;
    result.resize( source.length() );
    std::transform( source.begin(), source.end(), result.begin(), 
        std::ptr_fun<int, int>( std::toupper ) );
    return result;
}
Run Code Online (Sandbox Code Playgroud)

和...

std::string ToUpper( const std::string& source )
{
    std::string result( source.length(), '\0' );
    std::transform( source.begin(), source.end(), result.begin(), 
        std::ptr_fun<int, int>( std::toupper ) );
    return result;
}
Run Code Online (Sandbox Code Playgroud)

区别在于第一个使用reserve默认构造函数之后的方法,但第二个使用接受字符数的构造函数.

编辑 1.我不能使用boost lib.我只是想在构造函数之间的分配和构造函数之后分配之间进行比较.

c++ string stl

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