C ++: optimize the use of template variables

I currently have the code as follows

template<typename Type>
Type* getValue(std::string name, bool tryUseGetter = true)
{
    if(tryUseGetter)
    {
        if(_properties[name]->hasGetter)
        {
            return (Type*)_properties[name]->getter();
        }
        return (Type*)_properties[name]->data;
    }
    else
    {
        return (Type*)_properties[name]->data;
    }
}

Is there a way to make tryUseGetter a timer compiler? those. move it to the template declaration so that it resembles this

template<typename Type, bool tryUseGetter = true>
...

Thanks.

+3
source share
5 answers

You can use the cast operator and structure getValueas follows (the usage syntax will be the same as with the function):

template<typename Type, bool tryUseGetter = true> 
struct getValue {};

template<typename Type>
struct getValue<Type, true> {
    getValue(const std::string& name) : name(name) {};
    operator Type*() const {
        if(_properties[name]->hasGetter) {
            return (Type*)_properties[name]->getter();
        }
        return (Type*)_properties[name]->data;
    }
private:
    const std::string& name;
};

template<typename Type>
struct getValue<Type, false> {
    getValue(const std::string& name) : name(name) {};
    operator Type*() const {
        return (Type*)_properties[name]->data;
    }
private:
    const std::string& name;
};

Using:

int main () {
    int* i = getValue<int>( "TEST" ); // true by default
    Xstruct* x = getValue<Xstruct, false>( "XS" ); // false
}
+2
source

Dirk struct. ( ), :

template<typename Type, bool tryUseGetter = true>
struct getValue;

template<typename Type>
struct getValue<Type, true>
{
    Type* run(std::string name)
    {
        if(_properties[name]->hasGetter)
        {
            return (Type*)_properties[name]->getter();
        }
        return (Type*)_properties[name]->data;
    }
};

template<typename Type>
struct getValue<Type, false>
{
    Type* run(std::string name)
    {
        return (Type*)_properties[name]->data;
    }
};

getValue<T>::run("foo") getValue<T, false>::run("foo").

100%, bool, , , int.

+6

"try-use-getter", :

struct __try_use_getter { }

external const __try_use_getter tryusegetter;

template<typename Type> 
Type* 
getValue(std::string name, const __try_use_getter&)
{
    if(_properties[name]->hasGetter)
    {
        return (Type*)_properties[name]->getter();
    }
    return (Type*)_properties[name]->data;
}

template<typename Type> 
Type* 
getValue(std::string name)
{
    return (Type*)_properties[name]->data;
}

:

int result = getValue("foo", tryusegetter);

,

int result = getValue("foo");

.

+5

( , , ),

template<typename Type>
Type* getValue(std::string const &name)
{
    if(_properties[name]->hasGetter)
    {
        return (Type*)_properties[name]->getter();
    }
    return (Type*)_properties[name]->data;
}

template<typename Type, bool tryUseGetter>
Type *getValue(std::string const &name) 
{
    if(tryUseGetter)
    {
        return getValue<Type>(name);
    }
    else
    {
        return (Type*)_properties[name]->data;
    }
}

In addition, you must first follow the real rules: Pass nameby-const-reference instead of passing a copy, for example.

+4
source

Before you go and make the code complicated ... did you check if the optimization compiler really does this for you?

0
source

Source: https://habr.com/ru/post/1716985/


All Articles