variable parameter function, how to make it type safe and more meaningful?

I am a newer for C++, and my first language is Chinese, so my words with English may be unmeaningful, say sorry first. I know there is a way to write a function with variable parameters which number or type maybe different each calling, we can use the macros of va_list,va_start and va_end. But as everyone know, it is the C style. When we use the macros, we will lose the benefit of type-safe and auto-inference, then I try do it whit C++ template. My work is followed:


#include<iostream>
#include<vector>
#include<boost/any.hpp>

struct Argument
{
    typedef boost::bad_any_cast bad_cast;

    template<typename Type>
    Argument& operator,(const Type& v)
    {
        boost::any a(v);
        _args.push_back(a);
        return *this;
    }

    size_t size() const
    {
        return _args.size();
    }

    template<typename Type>
    Type value(size_t n) const
    {
        return boost::any_cast<Type>(_args[n]);
    }

    template<typename Type>
    const Type* piont(size_t n) const
    {
        return boost::any_cast<Type>(&_args[n]);
    }
private:
    std::vector<boost::any> _args;
};

int sum(const Argument& arg)
{
    int sum=0;
    for(size_t s=0; s<arg.size(); ++s)
    {
        sum += arg.value<int>(s);
    }

    return sum;
}

int main()
{
    std::cout << sum((Argument(), 1, 3, 4, 5)) << std::endl;

    return 0;
}

I think it's ugly, I want to there is a way to do better? Thanks, and sorry for language errors.


You can do something like this:

template <typename T>
class sum{
    T value;
    public:
    sum ()
            : value() {};
    // Add one argument
    sum<T>& operator<<(T const& x)
            { value += x; return *this; }
    // to get funal value
    operator T()
            { return value;}
    // need another type that's handled differently?  Sure!
    sum<T>& operator<<(double const& x)
            { value += 100*int(x); return *this; }
};

#include <iostream>

int main()
{
    std::cout << (sum<int>() << 5 << 1 << 1.5 << 19) << "n";
    return 0;
}

Such technique (operator overloading and stream-like function class) may solve different problems with variable arguments, not only this one. For example:

create_window() << window::caption - "Hey" << window::width - 5;
     // height of the window and its other parameters are not set here and use default values

I wrote an article about a typesafe printf C++ implementation on DDJ.com some years ago. It propably deals with comparable problems. Maybe that helps.

See http://www.ddj.com/cpp/184401999


After giving it some thought, I found a way to do it using a typelist. You don't need an any type that way, and your code becomes type-safe.

It's based on building a template structure containing a head (of a known type) and a tail, which is again a typelist. I added some syntactic sugar to make it more intuitive: use like this:

// the 1 argument processing function
template< typename TArg > void processArg( const TArg& arg ) {
  std::cout << "processing " << arg.value << std::endl;
}

// recursive function: processes 
// the first argument, and calls itself again for 
// the rest of the typelist
// (note: can be generalized to take _any_ function
template< typename TArgs >
void process( const TArgs& args ) {
  processArg( args.head );
  return process( args.rest );
}

template<> void process<VoidArg>( const VoidArg& arg ){}

int main() {
  const char* p = "another string";
  process( (arglist= 1, 1.2, "a string", p ) );
}

And here is the argument passing framework:

#include <iostream>

// wrapper to abstract away the difference between pointer types and value types.    
template< typename T > struct TCont {
  T value;
  TCont( const T& t ):value(t){}
};

template<typename T, size_t N> struct TCont< T[N] > {
  const T* value;
  TCont( const T* const t ) : value( t ) { }
};

template<typename T> struct TCont<T*> {
  const T* value;
  TCont( const T* t ): value(t){}
};


// forward definition of type argument list
template< typename aT, typename aRest >
struct TArgList ;

// this structure is the starting point 
// of the type safe variadic argument list
struct VoidArg {

  template< typename A >
  struct Append {
    typedef TArgList< A, VoidArg > result;
  };

  template< typename A >
  typename Append<A>::result append( const A& a ) const {
    Append<A>::result ret( a, *this );
    return ret;
  }

  //syntactic sugar
  template< typename A > typename Append<A>::result operator=( const A& a ) const { return append(a); }

} const arglist;



// typelist containing an argument 
// and the rest of the arguments (again a typelist)
//
template< typename aT, typename aRest >
struct TArgList {
  typedef aT T;
  typedef aRest Rest;
  typedef TArgList< aT, aRest > Self;

  TArgList( const TCont<T>& head, const Rest& rest ): head( head ), rest( rest ){}

  TCont<T> head;
  Rest rest;

  template< typename A > struct Append {
    typedef TArgList< T, typename Rest::Append<A>::result > result;
  };

  template< typename A >
  typename Append< A >::result append( const A& a ) const {
    Append< A >::result ret ( head.value, (rest.append( a ) ) );
    return ret;
  }

  template< typename A > typename Append<A>::result operator,( const A& a ) const { return append(a); }
};
链接地址: http://www.djcxy.com/p/43116.html

上一篇: 我可以在SQL Server 2008中创建“覆盖,空间”索引吗?

下一篇: 可变参数函数,如何使它更安全更有意义?