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

Blog


    8/5/2009

    Visual Studio 2010 及 C++ 0x、C# 4.0(二)

    前面说到 C++ 0x 的 Lambda 表达式,前几天我总是感觉它缺了点什么,但说不出来。
    现在觉得可能可以表达出来了。那就是,C++ 中没有明确的“委托”类型。
    (为了表达这个概念,又不与 C++ 中已有概念混淆,这里请允许我使用 C# 中的名词。)
     
    其实也不是没有,C++ 的函数指针其实就是形式上很精确的“委托”类型。虽然它归根结底却是一个模糊得不能再模糊的普通指针而已,但起码,一个这样的函数放在那里:

    void sort(int arr[], int size, bool (*comparer)(int, int));

    给人的信息是非常明确且严格的。
    但是如上篇所试验,不能像这样传入 Lambda 表达式:

    sort(arr, N, [](int a, int b){ return a >= b; });

    而只能先定义一个小函数,然后使用:

    bool greater_than(int a, int b)

    {

        return a >= b;

    }

    sort(arr, N, greater_than);

    但是 Lambda 的引入,不就是为了解决这种小函数的问题的吗?
    另外,如果把函数改为

    template <typename T>

    void sort(int elem[], int n, T comparer); 

    这样,虽然(可能)确实可以 sort(arr, N, [](int a, int b){ return a >= b; }); ,
    但是实际使用的时候,我却不能知道 comparer 到底要符合什么样的形式。
     
    而 C# 中,由于有明确的委托类型存在,这种问题也就不存在了:

    delegate bool BinaryIntComparer(int a, int b);

    void sort(int[] arr, BinaryIntComparer comparer);

    使用的时候:

    sort(arr, (int a, int b) => a >= b);

     
    所以,对于 C++ 0x 来说,作为语法糖的 Lambda,似乎还不够甜……
     
    ==============================华丽的分隔线==============================
    今天随意看了一下 C# 4.0 的特性。所谓的协变性和逆变性一下子还看不明白。。。(果然 C# 是用来玩设计的)
    动态特性倒是理解了一点点:
     

    class Plane

    {

        public void Fly()

        {

            Console.WriteLine("Plane flies.");

        }

    }

     

    class Car

    {

        public void Run()

        {

            Console.WriteLine("Car runs");

        }

    }

     

    class Bird

    {

        public void Fly()

        {

            Console.WriteLine("Bird flies.");

        }

    }

     

    class Program

    {

        static void LetItFly(dynamic thing)

        {

            try

            {

                thing.Fly();

            }

            catch

            {

                Console.WriteLine("Method \"Fly()\" does not exists.");

            }

        }

     

        static void Main(string[] args)

        {

            LetItFly(new Plane());

            LetItFly(new Car());

            LetItFly(new Bird());

        }

    }

     
    结果:
    Plane flies.
    Method "Fly()" does not exists.
    Bird flies.
     
    我们所折腾的就是被声明为 dynamic 类型的 thing。“thing.Fly();”中的句点后面可以随意写个方法名称,编译的时候不来检查,到运行的时候才检查。
    如果没有 dynamic,参数就要被声明为 object,然后根据反射特性,去查询 Fly 方法有没有……
    看起来 dynamic 只是把这一堆繁琐的过程省略了。
     
    至于网上牛人们说的动态语言静态语言大融合这高度上的东东,我暂时是没有体会到,大概是我动态语言没怎么玩过的缘故吧。。。呃……JavaScript 也是动态语言?我怎么没感觉呢。。。
     
     
     

    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!360.trak
    Weblogs that reference this entry
    • None