Skip to content

Instantly share code, notes, and snippets.

@yni3
Last active April 23, 2019 23:36
Show Gist options
  • Save yni3/b79d136fb35f306dfc901f167c608898 to your computer and use it in GitHub Desktop.
Save yni3/b79d136fb35f306dfc901f167c608898 to your computer and use it in GitHub Desktop.
C++ : concat Variadic parameters (float,int,string) to string
/*
stringify.h -- [C++11] concat varaiadic paramaters to string
April 22th, 2019
Copyright 2019 yni3
License : Public Domain
*/
#pragma once
#ifndef __STRINGIFY_H__
#define __STRINGIFY_H__
#include <string>
#include <sstream>
#include <utility>
namespace yni3
{
using std::stringstream;
using std::string;
struct inner
{
void concat( stringstream& )
{
}
template <typename Head, typename... Tail>
decltype( static_cast<long long>( std::declval<Head>() )
, void() )
concat( stringstream& ss, Head&& h, Tail&& ... t )
{
ss << static_cast<long long>( std::forward<Head>( h ) );
concat( ss, std::forward<Tail>( t )... );
}
template <typename Head, typename... Tail>
decltype( string( std::declval<Head>() )
, void() )
concat( stringstream& ss, Head&& h, Tail&& ... t )
{
ss << std::forward<Head>( h );
concat( ss, std::forward<Tail>( t )... );
}
};
//concat Variadic parameters to string
template<typename... Args>
string stringify( Args&& ... args )
{
stringstream ss;
inner i;
i.concat( ss, std::forward<Args>( args )... );
return ss.str();
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment