2

I have to admit that I steer clear of templates as much as possible. I want to change that. They shouldn't be so scary.

If I have functions declared like:

std::vector<SQLFieldObject> executeSelectQueryReturnSingleInt(std::string _sql);
std::vector<SQLPlantObject> executeSelectQueryReturnSingleInt(std::string _sql);
std::vector<SQLOrderObject> executeSelectQueryReturnSingleInt(std::string _sql);   

Where in executeSelectQueryReturnSingleInt(std::string _sql) The code is exactly the same for each function. I create a local version of the std::vector and return it.

I'd like to just have one generic executeSelectQueryReturnSingleInt(std::string _sql) function.

Templates might solve this, correct? Looking at example templates

template <class SomeType>
SomeType sum (SomeType a, SomeType b)
{
     return a+b;
}

What is confusing is that the parameters going in are the same, except the return vector is different.

Can anyone help me understand how to start applying templates to code reuse?

2
  • 3
    My question is how do you overload functions with different return types only?
    – xis
    Apr 18, 2014 at 18:36
  • @XiaogeSu Just edit to clarify ... Apr 18, 2014 at 18:49

2 Answers 2

4

You can do that:

template<typename SQLObject>
std::vector<SQLObject> executeSelectQueryReturnSingleInt(std::string _sql) {
    ...
}

then you call it as, for example,

executeSelectQueryReturnSingleInt<SQLPlantObject>("rose");

Note that you have to explicitly specify the template arguments because they can't be deduced from the function arguments, but otherwise this works like you'd expect.

8
  • So would I create a SQLObject class and change my SQLPlantObject to subclass SQLObject? I otherwise dont see where SQLObjet would come from
    – Jasmine
    Apr 18, 2014 at 18:45
  • 1
    Nope, SQLObject is just a placeholder name (like an argument name). Actually, the norm is to use typename T, but I chose a different name here so that it makes the intended usage more clear.
    – nneonneo
    Apr 18, 2014 at 18:46
  • @nneonneo Did you mean to address the questions OP? Apr 18, 2014 at 18:55
  • @nneoneo Using name T might be a norm, but using typename sure is not, especially considering there are cases where class is required instead of typename.
    – Chnossos
    Apr 18, 2014 at 19:04
  • 3
    @Chnossos You are very much mistaken: typename is, if anything, used more often in my experience than class specifically to distinguish between the cases which require class and those that allow class or typename to be used interchangeably.
    – Casey
    Apr 18, 2014 at 19:07
2

In this particular case, the template parameter would be the return type. You then have to use it explicitly when calling the function because it cannot be deduced from the function parameters:

template <typename ReturnType>
ReturnType executeSelectQueryReturnSingleInt(std::string sql)
{
     return ReturnType(args....);
}

Then

auto x = executeSelectQueryReturnSingleInt<std::vector<SQLFieldObject>>(sqlstr);

If you only want to return an std::vector of a particular type, then

template <typename SQLObjType>
std::vector<SQLObjType> executeSelectQueryReturnSingleInt(std::string sql)
{
     return std::vector<SQLObjType>(args....);
}

and

auto x = executeSelectQueryReturnSingleInt<SQLFieldObject>(sqlstr);
2
  • 1
    I think template<typename ReturnObjectType> std::vector<ReturnObjectType> executeSelectQueryReturnSingleInt(const std::string& sql) might be better.
    – xis
    Apr 18, 2014 at 18:37
  • 1
    @XiaogeSu I added that option. Apr 18, 2014 at 18:39

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.