津津's profile溪流漫话BlogLists Tools Help

Blog


    7/29/2009

    Visual Studio 2010 及 C++ 0x(一)

    我记得之前下过 Visual Studio 2010 CTP 的,昨晚翻来翻去找不着了,只好重新下过。最新版本 beta1 了。
    之前看到过 C++ 0x 的一些新特性,忍不住试一试。
     
    首先是 lambda 表达式。网上有很多介绍 C++ 0x 特性的文章,其中关于 lambda 表达式的文章几乎是千篇一律的

    std::for_each(v.begin(), v.end(), [](int n) { cout << n << " "; });

    比较感兴趣的是 lambda 表达式本身的类型,也就是,上述 for_each 中的第三个参数该怎么声明?
     
    试验一:

    bool greater_than(int a, int b, bool (*comparer)(int, int))

    {

        return comparer(a, b);

    }

     

    int _tmain(int argc, _TCHAR* argv[])

    {

        bool b = greater_than(1, 2, [](int a, int b)->bool { return a > b; });

     

        return 0;

    }

     
    编译不过:
    Error 1 error C2664: 'greater_than' : cannot convert parameter 3 from '`anonymous-namespace'::<lambda0>' to 'bool (__cdecl *)(int,int)'
     
    可见 Lambda 表达式不能向下兼容到函数指针。
    看了一下 std::for_each 的定义,它的第三个参数是一个 functor。所以,
     
    试验二:

    template <typename Comparer>

    bool greater_than(int a, int b, Comparer compare)

    {

        return compare(a, b);

    }

     

    int _tmain(int argc, _TCHAR* argv[])

    {

        bool b = greater_than(1, 2, [](int a, int b)->bool { return a > b; });

     

        return 0;

    }

     
    编译通过,运行也正确。
     
    所以,似乎 Lambda 表达式的本质是 functor?
     
    有点怀疑能否进行严格的类型检查在上例中,故意传给 greator_than 一个不实现 operator()(int, int) 的对象,即
     
    试验三:

    template <typename Comparer>

    bool greater_than(int a, int b, Comparer compare)

    {

        return compare(a, b);

    }

     

    int _tmain(int argc, _TCHAR* argv[])

    {

        bool b = greater_than(1, 2, [](int a)->bool { return true; });

     

        return 0;

    }

     
    这里这个 Lambda 表达式只接受一个参数,意味着 Comparer 只实现了 operator()(int),而没有实现 operator()(int, int)。
     
    结果:
    Error 2 error C2064: term does not evaluate to a function taking 2 arguments
     
    突然醒悟了一下,觉得自己灰常灰常土了,C++ 的 functor 应该本来就是强类型的,支持编译期类型检查的吧?
    怪不得那些库都抛开了函数指针,而使用 functor 了。
     
    ==============================华丽的分隔线==============================
     
    强类型 enum 似乎 VS2010 里没有实现(据说 g++ 4.4 实现了)?怎么试怎么不行

    enum Enum1 : double

    {

        a1,

        b1,

        c1

    };

     

    enum class Enum1

    {

        a2,

        b2,

        c2

    };

     
    都通不过……
     
    [待续]

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://kejinjin.spaces.live.com/blog/cns!B1E9144DD8637DFA!359.trak
    Weblogs that reference this entry
    • None