0

Suppose I have a template

template <unsigned int SIZE=512>
structure Sql
{ 
      const char * operator()(const char* format, ...)
        {
               deal format string here ...
                return sql;
         }
private:
         char sql[SIZE];
}

I have another function:

int dbSelect (const char* sql, void *outdata)
{
    use sql to query database here...
}

void main()
{
    dbSelect(Sql<>()(“select data from abc where column =%u”,32u),&outdata);
}

Use Sql like this is safe? Sql<>() will create a temporary object ,what is the temporary object’ life time? During the dbSelect body,the temporary object’s sql[512] is still available to access? Is still valid to access?

vsoftco
  • 55,410
  • 12
  • 139
  • 252

1 Answers1

3

Yes it is safe, the lifetime of the temporary Sql<>() object is prolonged until the end of the full expression (the semicolon in your case):

dbSelect(Sql<>()(“select data from abc where column =%u”,32u),&outdata);
                                                                        ^^^^ 
                                                      here Sql<>() ceases to be valid

Related: C++: Life span of temporary arguments?

vsoftco
  • 55,410
  • 12
  • 139
  • 252