[Development] API for multiple (but variable number of) return arguments

Philip Ashmore contact at philipashmore.com
Sat Jul 27 23:28:25 CEST 2013


On 27/07/13 13:55, Andre Somers wrote:
> Hi,
>
> Op 27-7-2013 14:05, Philip Ashmore schreef:
>> Hi there.
>>
>> On 27/07/13 10:36, Andre Somers wrote:
>>> 1) pointers to ints
>>> bool getUnits(int* years, int* monts = 0, int* weeks = 0, int* days = 0,
>>> int* hours = 0, int* minutes = 0, int* seconds = 0, int* milliSeconds = 0);
>> You could wrap this into a struct:
>>     struct TimeSpanResult {
>>        int m_years, m_months, m_weeks, m_days, m_hours, m_minutes,
>> m_seconds, m_milliseconds;
>>     };
>>
>> and return it from the call:
>> TimeSpanResult getUnitsTimeSpan(Qt:Minutes | Qt:Seconds);
>>
>> I just used a different name as the return type can't be overloaded.
>>
>>
>> TimeSpanResult getUnitsTimeSpan(int flags)
>> {
>>      TimeSpanResult res;
>>      getUnits((flags & Qt:Years) ? & res.m_years : (int *)0, (flags &
>> Qt:Years) ? & res.m_months : (int *)0, ...
>>      return res;
>> }
>> I've left out error checking for clarity.
> Thanks for your suggestion. It is almost the same as option 2, only you 
> replaced the map with a purpose-made struct. I think that indeed that 
> might be beneficial to do.
>
> Note that the different name is not needed; I'd like to only expose 
> *one* API for this, and not multiple different ones. If I'd choose your 
> suggestion, there would be no name clashes with other functions doing 
> the same thing. In this case, there would be no clash either, as the 
> arguments are different as well as the return type.
Sorry for mylack of clarity.
I'm not developing Qt code at the minute and forgot about the Qt way of
passing flags.
The clash is with
   QMap<Qt::TimeSpanUnit, int> getUnits(Qt::TimeSpanFormat);

   QTimeSpanResult getUnits(Qt::TimeSpanFormat);
would clash.

Also, the proposal I'm making is an inline wrapper for option 1, so the
compiler would eliminate the wrapper completely.

>
>
> André
>
> _______________________________________________
> Development mailing list
> Development at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/development
Philip



More information about the Development mailing list