C++ variadic functions

Status
Not open for further replies.

mhayenga

Wise, Aged Ars Veteran
192
Currently I've written up some C++11 code that uses variadic templates to allow for a variable number of arguments with type information. This code works fine as is, but now I've realized that C++0x/11 won't work as I need to merge this into a larger code base & getting it to compile with "-std=c++0x" is easier said than done. So, I'm wondering whats the closest I can come to the following C++11 code without using C++11 features.

Function calling:
Code:
// Creating a structure
traceMem = new MemStruct(name, sets, ways,
               "PhysAddr", "0x%08x", 0xDEADBEEF,
               "Dirty",    "%1d",     false,
               "Valid",    "%1d",     false,
               "Data",     "0x%08x", 0x0);

// Later using it
traceMem->update(set, way, "PhysAddr", newAddr,
                           "Valid", true);
So, in addition to some basic parameters, the user specifies any number of "fields" that all contain 3 parts: 1) A string identifier 2) how they want the data presented (standard format string) and 3) the default value. Later they may post events using the string identifier and providing new data (which will be formatted with the previously provided format string).

The code that enables this looks something like:
Code:
// CRTP helper class that does the actual work
template<typename Derived>
class Fields
{
  protected:
    template<typename DataType, typename ...Params>
    void addField(const std::string& name, const std::string& format,
        const DataType& default_value, const Params& ...params)
    {
        static_assert(sizeof ...(params) % 3 == 0,
            "Wrong number of parameters to addField()");

        ... omitted code ...

        addField(params...);
    }

    void addField(void) {  }
  private:
    ... omitted code ...
};


// Real class
class MemStruct : public Fields<MemStruct>
{
  ... omitted code ...
}
With this I more or less have an easy way of letting users define a table (a processor cache in this case), where they can specify any number of "Fields" per entry, along with how they want the data to be formatted, and being able to submit data updates at a later time.

The 2 options I currently see using "standard" c++ are...

1) Give up on variable number of parameters and go to code like this (with the last parameter being a template parameter
Code:
// Creating a structure
traceMem = new MemStruct(name, sets, ways);
traceMem->addField("PhysAddr", "0x%08x", 0xDEADBEEF);
traceMem->addField("Dirty",    "%1d",     false);
traceMem->addField("Valid",    "%1d",     false);
traceMem->addField("Data",     "0x%08x", 0x0);

// Later using it
traceMem->update(set, way, "PhysAddr", newAddr);
traceMem->update(set, way, "Valid", true);
2) Defining a *large* number of versions of the function myself
Code:
// One argument
template<typename T1> void addField(const std::string& name, const std::string& format, const T1& val);
// Two arguments
template<typename T1, typename T2> void addField(const std::string& name1, const std::string& format1, const T1& val1,
const std::string& name2, const std::string& format2, const T2& val2);
.... (until I think I have more than anyone would really use)

I probably need to format/word this post better... I can code something that "works", but am just wondering is there a better way?
 

chaboud

Ars Centurion
313
Subscriptor
Well.... You could do chained templates, that contain themselves and one child.

But, interestingly enough, you actually have enough information, except for a count to use traditional va_args. You could make a simple set of templates up to, say 9 args, that calls a variadic function with a template-derived count.

Code:
void Do(int X, ...)
{
  //va_args stuff
}

template <typename A>
void CallDo(A AIn)
{
  Do(1, AIn);
}

template <typename A, typename B>
void CallDo(A AIn, B BIn)
{
  Do(2, AIn, BIn);
}

You could get templaty, functory, and pointery on this, and, most importantly, it horks safety, but...

Honestly, I'd consider doing something with templates that picks off tuples and chains their insertion, effectively building a linked (or contained) list of args, but syntax can get hairy, fast. "> > >" vs ">> >" etc. You'll have to make adapter templates (as you've pointed out), but I think it's the cleanest thing by far. Then, when you can switch compilers, the adapter templates are gone.
 
Status
Not open for further replies.