Skip to main content
added 1 character in body
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341
    lambda((std::vector<int>::iterator out)
    {
        std::cout << *out; 
        return *out; 
    }, 1);

    // The equivalent without the overhead of any new operations.
    classstruct exp1 {
       int operator()(std::vector<int>::iterator out) const {
            std::cout << *out;
            return *out;
       }
    };
    lambda((std::vector<int>::iterator out)
    {
        std::cout << *out; 
        return *out; 
    }, 1);

    // The equivalent without the overhead of any new operations.
    class exp1 {
       int operator()(std::vector<int>::iterator out) const {
            std::cout << *out;
            return *out;
       }
    };
    lambda((std::vector<int>::iterator out)
    {
        std::cout << *out; 
        return *out; 
    }, 1);

    // The equivalent without the overhead of any new operations.
    struct exp1 {
       int operator()(std::vector<int>::iterator out) const {
            std::cout << *out;
            return *out;
       }
    };
added 804 characters in body
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341
    lambda((std::vector<int>::iterator out)
    {
        std::cout << *out; 
        return *out; 
    }, 1);

    // The equivalent without the overhead of any new operations.
    class exp1 {
       std::vector<int>::iteratorint operator()(std::vector<int>::iterator out) const {
            std::cout << *out;
            return *out;
       }
    };

###It does not compile:

bd.cpp:183:16: note: to match this '('
        lambda((std::vector<int>::iterator out)
               ^
bd.cpp:185:27: error: use of undeclared identifier 'out'; did you mean 'oct'?
            std::cout << *out;
                          ^~~
                          oct
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ios:956:1: note:
      'oct' declared here
oct(ios_base& __str)
^
bd.cpp:185:30: error: expected '}'
            std::cout << *out;
                             ^
bd.cpp:184:9: note: to match this '{'
        {
        ^
bd.cpp:188:9: error: use of undeclared identifier 'exp1'
        exp1(iterator);
    lambda((std::vector<int>::iterator out)
    {
        std::cout << *out; 
        return *out; 
    }, 1);

    // The equivalent without the overhead of any new operations.
    class exp1 {
       std::vector<int>::iterator operator()(std::vector<int>::iterator out) const {
            std::cout << *out;
            return *out;
       }
    };
    lambda((std::vector<int>::iterator out)
    {
        std::cout << *out; 
        return *out; 
    }, 1);

    // The equivalent without the overhead of any new operations.
    class exp1 {
       int operator()(std::vector<int>::iterator out) const {
            std::cout << *out;
            return *out;
       }
    };

###It does not compile:

bd.cpp:183:16: note: to match this '('
        lambda((std::vector<int>::iterator out)
               ^
bd.cpp:185:27: error: use of undeclared identifier 'out'; did you mean 'oct'?
            std::cout << *out;
                          ^~~
                          oct
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ios:956:1: note:
      'oct' declared here
oct(ios_base& __str)
^
bd.cpp:185:30: error: expected '}'
            std::cout << *out;
                             ^
bd.cpp:184:9: note: to match this '{'
        {
        ^
bd.cpp:188:9: error: use of undeclared identifier 'exp1'
        exp1(iterator);
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341

Will this is silly:

#define public_functions  public
#define public_member     public
#define public_classes    public
#define private_functions private
#define private_member    private
#define private_classes   public
  • Does not give you an more real extra information
  • Break Syntax Highlighting
  • Makes anybody reading the code go uhhhh
  • Is not enforced by the language.
  • The last one is public why why why

.

#define private_classes   public   // WHY WHY WHY

###Exceptions

    class bad_cast
    {
    public_functions:
        bad_cast()
            : m_Msg("bad_cast: invalid conversion of a Whatever object")
        {}

        const char* what()
        {
            return m_Msg;
        }

    private_member:
        const char* m_Msg;
    };

There is already a std::bad_cast why re--invent it.

Exceptions should probably inherit from std::runtime_error if you are going to write them (that way you can use there version of what() and storage).

Talking of what() it should definitely by marked cost. And under no circumstances should it throw. So the actual declaration you want is:

        const char* what() const throws (); // or noexcept on modern compiler

###class Whatever

The copy constructor

    Whatever& operator = (const Whatever& what)

Does not provide the strong exception guarantee. Learn to use the copy and swap idiom it will save you.

Stop using Yoda Conditionals

        if (nullptr != m_WrappedValue)

This technique is like 20 years old. It was not popular then it is way out dated now. It adds a cognitive burden to the reader that is not necessary. It provides no actual benefits. What you are trying to achieve (detection of accidental assignment) can be done by the compiler already.

###This is useless trick

The same affect can be achieved by just declaring a class.

    lambda((std::vector<int>::iterator out)
    {
        std::cout << *out; 
        return *out; 
    }, 1);

    // The equivalent without the overhead of any new operations.
    class exp1 {
       std::vector<int>::iterator operator()(std::vector<int>::iterator out) const {
            std::cout << *out;
            return *out;
       }
    };

Also this does not have the disadvantage of a macro. Which is a really confusing message if you add a comma anywhere in the code block.