boost :: bind不能用于条件表达式?

use*_*210 6 c++ boost

当我取消注释条件表达式时,程序将无法在visual c ++ 2008下编译.

#include <iostream>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
typedef boost::function<void(int, int)> vii_t;
typedef boost::function<void(int)> vi_t;

void foo(int a, int b){}
void bar(int a){}
int main(int argc, char* argv[])
{
    //vi_t test= true ? boost::bind(foo, _1, 100) : boost::bind(bar, _1);
    vi_t test1 = boost::bind(foo, _1, 100);
    vi_t test2 = boost::bind(bar, _1);
    //test(1);
    test1(1);
    test2(1);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Bo *_*son 1

在表达式中,c ? x : yx 和 y 必须是同一类型,或者其中一个必须可以转换为另一个。该公共类型是整个表达式的类型。

据推测,boost::bind不同数量的参数返回不同的类型,这些类型不能相互转换。它们都可以转换成vi_t没有帮助。

  • 我同意——作为一个测试,如果将三元运算符最右边的两个表达式转换为 vi_t,它应该可以编译。 (2认同)