C++ Logo

std-proposals

Advanced search

Proposal: add support for template qualifiers

From: Oleksii Tarasiuk <o.tarasiuk_at_[hidden]>
Date: Mon, 17 Feb 2020 22:21:18 +0100
Hi all!

/////////////////////// Example 1:

struct Bar{
        string name;
        // ...
};

//------------------ with template const:
class Foo{
        vector<Bar> bars_;
        bool dirty_ = false;
public:
        template<bool b>
        Bar const<b>* byName(string const& name) const<b> {
                for(auto& bar: bars_) {
                        if(bar.name == name) {
                                if constexpr (b)
                                        dirty_ = true;
                                return &bar;
                        }
                }
                return nullptr;
        }

        bool dirty() const { return dirty_; }
};

//------------------ as usual (copy/paste technology):
class Foo{
        vector<Bar> bars_;
        bool dirty_ = false;
public:
        Bar const* byName(string const& name) const {
                for(auto& bar: bars_) {
                        if(bar.name == name) {
                                return &bar;
                        }
                }
                return nullptr;
        }

        Bar* byName(string const& name) {
                for(auto& bar: bars_) {
                        if(bar.name == name) {
                                dirty_ = true;
                                return &bar;
                        }
                }
                return nullptr;
        }

        bool dirty() const { return dirty_; }
};

/////////////////////// Example 2:
template<class T, bool isThreadLocal>
class Singleton {
private:
      static thread_local<isThreadLocal> T* this_;
};

/////////////////////// Example 3:
template<class Fun, bool isStatic>
struct Foo: public Fun {
       static<isStatic> void bar() {
              if constexpr (isStatic)
                  Fun::foo();
              else{
                  this->foo();
              }
       }
};

Thanks.

Best regards,
Oleksii Tarasiuk mailto:o.tarasiuk_at_[hidden]

Received on 2020-02-17 15:24:05