Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
a4z_blog_2017_08_18_v1
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//g++ 5.4.0 #include <iostream> #include <type_traits> template<typename WANTED, typename...> struct can_be_used_as : std::true_type {}; template<typename WANTED, typename HEAD, typename... TAIL> struct can_be_used_as<WANTED, HEAD, TAIL...> : std::integral_constant<bool, (std::is_same<WANTED,HEAD>{} || std::is_base_of<WANTED,HEAD>{} || std::is_convertible<HEAD,WANTED>{}) && can_be_used_as<WANTED, TAIL...>{}> {}; template<typename... strings> std::string join_with(const std::string& what, const std::string& head, const strings&... tail) { static_assert(can_be_used_as<std::string, strings...>{}, "only std::string compatible arguments allowed, " "please convert given arguments to string"); std::string out{head}; auto join = [&](const auto& s){out+= what+s; }; (void)join ; // maybe unused, in case of join_with ("/", "a") ; int dummy[] = { 0, ( (void) join(tail), 0) ... }; (void)dummy; // for sure unused return out ; } int main () { const std::string a{"a"}; std::cout << "JOIN " << join_with("/", a , "hurra"); // uncomment to see the error message //std::cout << "JOIN " << join_with("\\", "2", 2) << std::endl;; return 0 ; }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 0.43 sec, absolute running time: 0.09 sec, cpu time: 0.03 sec, memory peak: 3 Mb, absolute service time: 0,53 sec
edit mode
|
history
JOIN a/hurra