Does C++ have “with” keyword like Pascal?

后端 未结 17 831
傲寒
傲寒 2020-12-06 05:02

with keyword in Pascal can be use to quick access the field of a record. Anybody knows if C++ has anything similar to that?

Ex: I have a pointer with m

相关标签:
17条回答
  • 2020-12-06 05:03

    No, C++ does not have any such keyword.

    0 讨论(0)
  • 2020-12-06 05:06

    C++ does not have a feature like that. And many consider "WITH" in Pascal to be a problem because it can make the code ambiguous and hard to read, for example it hard to know if field1 is a member of pointer or a local variable or something else. Pascal also allows multiple with-variables such as "With Var1,Var2" which makes it even harder.

    0 讨论(0)
  • 2020-12-06 05:08

    The following approach relies on Boost. If your compiler supports C++0x's auto then you can use that and get rid of the Boost dependence.

    Disclaimer: please don't do this in any code that must be maintained or read by someone else (or even by yourself in a few months):

    #define WITH(src_var)                                             \
        if(int cnt_ = 1)                                              \
            for(BOOST_AUTO(const & _, src_var); cnt_; --cnt_)
    
    
    int main()
    {
        std::string str = "foo";
    
        // Multiple statement block
        WITH(str)
        {
            int i = _.length();
            std::cout << i << "\n";
        }
    
        // Single statement block
        WITH(str)
            std::cout << _ << "\n";
    
        // Nesting
        WITH(str)
        {
            std::string another("bar");
            WITH(another)
                assert(_ == "bar");
        }
    }
    
    0 讨论(0)
  • 2020-12-06 05:09

    I like to use:

        #define BEGIN_WITH(x) { \
            auto &_ = x;
    
        #define END_WITH() }
    

    Example:

        BEGIN_WITH(MyStructABC)
        _.a = 1;
        _.b = 2;
        _.c = 3;
        END_WITH()
    
    0 讨论(0)
  • 2020-12-06 05:10

    No, there is no with keyword in C/C++.

    But you can add it with some preprocessor code:

    /* Copyright (C) 2018 Piotr Henryk Dabrowski, Creative Commons CC-BY 3.0 */
    
    #define __M2(zero, a1, a2, macro, ...) macro
    
    #define __with2(object, as) \
        for (typeof(object) &as = (object), *__i = 0; __i < (void*)1; ++__i)
    
    #define __with1(object) __with2(object, it)
    
    #define with(...) \
        __M2(0, ##__VA_ARGS__, __with2(__VA_ARGS__), __with1(__VA_ARGS__))
    

    Usage:

    with (someVeryLongObjectNameOrGetterResultOrWhatever) {
        if (it)
            it->...
        ...
    }
    
    with (someVeryLongObjectNameOrGetterResultOrWhatever, myObject) {
        if (myObject)
            myObject->...
        ...
    }
    

    Simplified unoverloaded definitions (choose one):

    unnamed (Kotlin style it):

    #define with(object) \
        for (typeof(object) &it = (object), *__i = 0; __i < (void*)1; ++__i)
    

    named:

    #define with(object, as) \
        for (typeof(object) &as = (object), *__i = 0; __i < (void*)1; ++__i)
    

    Of course the for loop always has only a single pass and will be optimized out by the compiler.

    0 讨论(0)
  • 2020-12-06 05:10

    First I've heard that anybody doesn't like 'with'. The rules are perfectly straightforward, no different from what happens inside a class in C++ or Java. And don't overlook that it can trigger a significant compiler optimization.

    0 讨论(0)
提交回复
热议问题