From 10f33eee1dfefa9e5a2ee5cf4946095bf08b19bc Mon Sep 17 00:00:00 2001
From: Stephen Kelly <steveire@gmail.com>
Date: Wed, 2 Apr 2014 15:02:41 +0200
Subject: [PATCH] Features: Add cxx_variadic_templates.

Expect cxx_variadic_templates to implement N2555.

N2555 is essentially a bugfix and predates most compiler releases which
aimed to experimentally support variadic templates.
---
 Help/variable/CMAKE_CXX_KNOWN_FEATURES.rst    |  5 ++
 Modules/Compiler/GNU-CXX-FeatureTests.cmake   | 13 ++++
 Source/cmMakefile.cxx                         |  3 +-
 .../cxx_variadic_templates.cpp                | 65 +++++++++++++++++++
 4 files changed, 85 insertions(+), 1 deletion(-)
 create mode 100644 Tests/CompileFeatures/cxx_variadic_templates.cpp

diff --git a/Help/variable/CMAKE_CXX_KNOWN_FEATURES.rst b/Help/variable/CMAKE_CXX_KNOWN_FEATURES.rst
index 957c7630b..b6b2b3589 100644
--- a/Help/variable/CMAKE_CXX_KNOWN_FEATURES.rst
+++ b/Help/variable/CMAKE_CXX_KNOWN_FEATURES.rst
@@ -21,3 +21,8 @@ The features known to this version of CMake are:
   Delegating constructors, as defined in N1986_.
 
   .. _N1986: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1986.pdf
+
+``cxx_variadic_templates``
+  Variadic templates, as defined in N2242_.
+
+  .. _N2242: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2242.pdf
diff --git a/Modules/Compiler/GNU-CXX-FeatureTests.cmake b/Modules/Compiler/GNU-CXX-FeatureTests.cmake
index 4f14b1781..6baa46345 100644
--- a/Modules/Compiler/GNU-CXX-FeatureTests.cmake
+++ b/Modules/Compiler/GNU-CXX-FeatureTests.cmake
@@ -5,7 +5,20 @@ set(_oldestSupported "(__GNUC__ * 100 + __GNUC_MINOR__) >= 408")
 # TODO: Should be supported by GNU 4.7
 set(GNU47_CXX11 "${_oldestSupported} && __cplusplus >= 201103L")
 set(_cmake_feature_test_cxx_delegating_constructors "${GNU47_CXX11}")
+# NOTE: C++11 was ratified in September 2011. GNU 4.7 is the first minor
+# release following that (March 2012), and the first minor release to
+# support -std=c++11. Prior to that, support for C++11 features is technically
+# experiemental and possibly incomplete (see for example the note below about
+# cxx_variadic_template_template_parameters)
 # TODO: Should be supported by GNU 4.4
 set(GNU44_CXX11 "${_oldestSupported} && __cplusplus >= 201103L")
 set(_cmake_feature_test_cxx_auto_type "${GNU44_CXX11}")
+set(_cmake_feature_test_cxx_variadic_templates "${GNU44_CXX11}")
+# TODO: If features are ever recorded for GNU 4.3, there should possibly
+# be a new feature added like cxx_variadic_template_template_parameters,
+# which is implemented by GNU 4.4, but not 4.3. cxx_variadic_templates is
+# actually implemented by GNU 4.3, but variadic template template parameters
+# 'completes' it, so that is the version we record as having the variadic
+# templates capability in CMake. See
+# http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2555.pdf
 set(_oldestSupported)
diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx
index de6b972bc..52f4fb698 100644
--- a/Source/cmMakefile.cxx
+++ b/Source/cmMakefile.cxx
@@ -43,7 +43,8 @@
 
 #define FOR_EACH_CXX_FEATURE(F) \
   F(cxx_auto_type) \
-  F(cxx_delegating_constructors)
+  F(cxx_delegating_constructors) \
+  F(cxx_variadic_templates)
 
 class cmMakefile::Internals
 {
diff --git a/Tests/CompileFeatures/cxx_variadic_templates.cpp b/Tests/CompileFeatures/cxx_variadic_templates.cpp
new file mode 100644
index 000000000..1d5a7061b
--- /dev/null
+++ b/Tests/CompileFeatures/cxx_variadic_templates.cpp
@@ -0,0 +1,65 @@
+template<int I, int... Is>
+struct Interface;
+
+template<int I>
+struct Interface<I>
+{
+  static int accumulate()
+  {
+    return I;
+  }
+};
+
+template<int I, int... Is>
+struct Interface
+{
+  static int accumulate()
+  {
+    return I + Interface<Is...>::accumulate();
+  }
+};
+
+// Note: split this into a separate test if a
+// cxx_variadic_template_template_parameters feature is added.
+
+template<typename T>
+struct eval {
+  enum {
+    Matched = 0
+  };
+};
+
+template<template<typename...> class T, typename... U>
+struct eval<T<U...> > {
+  enum {
+    Matched = 1
+  };
+};
+
+template<typename...>
+struct A {
+
+};
+template<typename T>
+struct B {
+
+};
+template<typename T, typename U>
+struct C {
+
+};
+template<typename T, typename U, typename...>
+struct D {
+
+};
+
+// Note: This test assumes that a compiler supporting this feature
+// supports static_assert. Add a workaround if that does not hold.
+static_assert(eval<A<> >::Matched, "A Matches");
+static_assert(eval<A<int> >::Matched, "A Matches");
+static_assert(eval<A<int, char> >::Matched, "A Matches");
+static_assert(eval<B<int> >::Matched, "B Matches");
+static_assert(eval<C<int, char> >::Matched, "C Matches");
+static_assert(eval<D<int, char> >::Matched, "D Matches");
+static_assert(eval<D<int, char, bool> >::Matched, "D Matches");
+static_assert(eval<D<int, char, bool, double> >::Matched, "D Matches");