2

I want to eliminate the process consumed by a decision using pointers to member functions. I need to give user options to turn on or off domain checking for a function defined on a limited continues domain.

It's ok to have pointers to member functions when not using templates. But here I have to generalize my implementation.

Specifically I have three member functions in my class:

1.value is a member function returns value calculated by the member that function points to. The function is a function pointer wich points to either checkedValue or uncheckedValue. 2.checkedValue is a member function that calculates and returns the result, if input is in specified range. else throws a std::domain error. 3.uncheckedValue calculates and returns the result, regardless of domain.

template <typename T>
class IO
{
private:
    typedef T (IO<T>::*functionPtr)(const std::string& name, const T& input) const;
    functionPtr function;
    bool domainValidation;
    void setDomainValidation(const bool& value);
    //...
public:
    // ...
    T value(const std::string& name, const T& input) const;
    T uncheckedValue(const std::string& name, const T& input) const;
    T checkedValue(const size_t& index, const T &input) const;
};

// Implementation:

template <typename T>
void IO<T>::setDomainValidation(const bool &value)
{
    domainValidation = value;
    if(domainValidation)
    {
        // function points to checkedValue()
        function =  & IO<T>::checkedValue; // Here I got undefinded reference error
    }
    else
    {
        // function points to uncheckedValue()
    }
}

template <typename T>
T IO<T>::value(const string &name, const T &input) const
{
    return (this->*function)(name,input);
}

template <typename T>
T IO<T>::uncheckedValue(const string &name, const T &input) const
{
    // simply calculate and return the result
}

template <typename T>
T IO<T>::checkedValue(const string &name, const T &input) const
{
    // if input is in domain, calculate and return the result
    // else throw a std::domain error
}
4
  • What is the exact error? Commented Jul 5, 2012 at 13:19
  • why not simply ... value(...) { if (domainValidation) return checkedValue(...) else return uncheckedValue(...) } Commented Jul 5, 2012 at 13:22
  • @MadScientist, because later he wants to reuse the function without doing the if every time. Commented Jul 5, 2012 at 13:27
  • @MadScientist because value will be called millions of times! I don't wan't to waste time on checking domainValidation wich is set only once (startup time). Commented Jul 5, 2012 at 13:32

2 Answers 2

4

This looks like a typo to me: The function signature of the function pointer is

...(const std::string &, const T &) ...

The signature of checkedValue is

...(const size_t &, const & T) ...

If the signature is changed to match the function pointer, the code sample compiles for me. Concerning the performance: Are you sure (as in have profiled or looked at the assembly) that the simple if-clause inside the value-method is worse than the (possible) indirection through a function pointer? Obviousl, the call to checked or uncheckedValue cannot be inlined.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 for a much more clear answer. Also, I have to agree that unless you measure it, you can't know if the if is better or function pointer indirection.
You're right. I can't say it will be definitely faster, without benchmarking application. But more specific derived classes may have more complex decisions, that they will only apply once. (may be more than ten if-then clauses in configuration stage). In addition I have no Idea about which data type should be used in IO. It may be a matrix with costly comparison operators.
3

Your function has signature

T checkedValue(const size_t& index, const T &input) const;

but not

T IO<T>::checkedValue(const string &name, const T &input) const;

Note the difference between the types of the first parameter.

4 Comments

The function is inside IO<T>, so no!
@Shanbaz, So what? Try to adding in class T checkedValue(const std::string& name, const T& input) const { return T(); } and delete T IO<T>::checkedValue(const string &name, const T &input) const not in class and saw result.
Oops! I've declared them but not implemented! Makes me feel dummy since I spent last 2 hours on such a simple fault :-/
@ForEveR, I didn't notice that you were pointing out the difference in the type of the first parameter! With a quick scan, I saw only IO<T> as the difference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.