C++ Logo

STD-PROPOSALS

Advanced search

Subject: [std-proposals] Proposal: add support for template qualifiers
From: Oleksii Tarasiuk (o.tarasiuk_at_[hidden])
Date: 2020-02-17 15:21:18


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]


STD-PROPOSALS list run by herb.sutter at gmail.com

Standard Proposals Archives on Google Groups