// 1 filename:cpp2011-14-8-2.cpp
// ver 0.1 June.12, 2014
//
// 2 original examples and/or notes:
// (c) ISO/IEC JTC1 SC22 WG21 N3242, April 12, 2011
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf
// >14 Templates 14.8 Function template specializations 14.8.2 Template argument deduction
//
// 3 compile and output mechanism:
// (c) Dr. OGAWA Kiyoshi, kaizen at gifu-u.ac.jp,
//
// 4 compile errors and/or warnings:
// 4.1(c) Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
// Target: x86_64-apple-darwin13.2.0,  Thread model: posix
// Command/Options: c++ -std=c++11 -stdlib=libc++ -Wall cpp2011-14-8-2.cpp 
// (c) LLVM 2003-2009 University of Illinois at Urbana-Champaign.

// 4.2. g++-4.9 (GCC) 4.9.0 20131229 (experimental)
// Copyright (C) 2013 Free Software Foundation, Inc.
// This is free software; see the source for copying conditions.  There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// http://gcc.gnu.org/onlinedocs/gcc/Standards.html
// Command/Options: g++-4.9  -std=c++11  -Wall cpp2011-14-8-2.cpp 
// g++-4.9: error: unrecognized command line option '-stdlib=libc++'
// Configuration:brew install gcc49
//
// 4.3. Visual Studio Express 2013, 
// (c) Microsoft http://www.visualstudio.com/
// SPEC:
// Windows 7, .NET Framework
// (c) VMware, Inc.
// VMWare fusion 6
//
// 5. Hardware:  MacBook Pro, 
//(c) Intel http://ark.intel.com/products/37006/
//Core 2 Duo 2.53GHz, 8GB, 1067MHz DDR3
//
// 6. Special Thanks: Upper organizatios and 
// ITSCJ/IPSJ http://www.itscj.ipsj.or.jp/itscj_english/index.html
// Renesas Electronics Corporation.http://www.renesas.com/
// NPO SESSAME project, http://www.sessame.jp/workinggroup/WorkingGroup3/
// Toyo Corporation, http://www.toyo.co.jp/English/
// Japan Standard Association, http://bit.ly/1lzykg1
// NPO TOPPERS project, https://www.toppers.jp/asp-d-download.html
// Daido Universcity, http://www.daido-it.ac.jp/gakubugakka/computer/index.html
// WITZ Co.Ltd., http://www.witz-inc.co.jp/products/solution/solution.html
// SevenWise.co., http://www.7ws.co.jp/index.html
// TOYOTA Motor Corporation, http://toyota.jp/
// IT planning Inc., http://www.itpl.co.jp/en/index.html
// DENSO Corporation, http://www.globaldenso.com/en/
// Aisin Seiki co. Ltd., http://www.aisin.com/
// Spancion Inc., http://www.spansion.com/
// Yazaki Corporation, http://www.yazaki-group.com/global/
// Pananosic Corporation, http://www.panasonic.net/
// SWEST: Summer Workshop on Embedded System Technologies , http://swest.toppers.jp
// CEST: Consortium for Embedded System Technology, http://www.ertl.jp/CEST/
// JUSE: Union of Japanese Scientists and Engineers, http://www.juse.or.jp/e/
// OSC:Open Source Conference, http://www.ospn.jp/

#include <iostream>
#include <cstdlib>
//#include <string.h>
#include <cstring>
#include <type_traits>
//#include <cassert>
#include <array>
#include <complex>
#include <ccomplex>

using namespace std;

typedef complex<double> dcomplex;//http://d.hatena.ne.jp/white_wheels/20100321/p1
typedef complex<int> icomplex;

void f(Array<dcomplex>& cv, Array<int>& ci) {
sort(cv); // calls sort(Array<dcomplex>&)
sort(ci); // calls sort(Array<int>&)
}
void g(double d) {
int i = convert<int>(d); // calls convert<int,double>(double)
int c = convert<char>(d); // calls convert<char,double>(double)
}
template <class T> void f(T t);
template <class X> void g(const X x);
template <class Z> void h(Z, Z*);
//
template <class T, class U = double>
void f2(T t = 0, U u = 0);
void g() {
f2(1, 'c'); // f<int,char>(1,’c’)
f2(1); // f<int,double>(1,0)
	//error: f2(); // error: T cannot be deduced
f2<int>(); // f<int,double>(0,0)
f2<int,char>(); // f<int,char>(0,0)
}
//
struct X { };
struct Y {
Y(X){}
};
template <class T> auto f(T t1, T t2) -> decltype(t1 + t2); // #1
X f(Y, Y); // #2
X x1, x2;
X x3 = f(x1, x2); // deduction fails on #1 (cannot add X+X), calls #2
//
template <class T> int f(T[5]);
int I = f<int>(0);
//error: int j = f<void>(0); // invalid array
//
template <class T> int f(typename T::B*);
int i = f<int>(0);
//
template <int I> struct X2 { };
template <template <class T> class> struct Z { };
template <class T> void f2(typename T::Y*){}
template <class T> void g2(X2<T::N>*){}
template <class T> void h2(Z<T::template TT>*){}
struct A {};
struct B { int Y; };
struct C {
typedef int N;
};
struct D {
typedef int TT;
};
//
template <class T> int f3(int T::*);
int i2 = f3<int>(0);
//
template <class T, T> struct S {};
template <class T> int f4(S<T, T()>*);
struct X3 {};
int i0 = f4<X3>(0);
//
template <class T, T*> int f(int);
//error: int i2 = f<int,1>(0); // can’t conv 1 to int*
//
template <int> int f5(int);
template <signed char> int f5(int);
//error: int i1 = f<1>(0); // ambiguous
//error: int i2 = f<1000>(0); // ambiguous
//
int main() {
	// #1: function type is f(int), t is non const
f<int>(1);
// #2: function type is f(int), t is const
f<const int>(1);
// #3: function type is g(int), x is const
g<int>(1);
// #4: function type is g(int), x is const
g<const int>(1);
// #5: function type is h(int, const int*)
h<const int>(1,0);

	// Deduction fails in each of these cases:
	//error: f2<A>(0); // A does not contain a member Y
	//error: f2<B>(0); // The Y member of B is not a type
	//error: g2<C>(0); // The N member of C is not a non-type
	//error: h2<D>(0); // The TT member of D is not a template
	cout << "14 Templates 14.8 Function template specializations 14.8.2 Template argument deduction"<<std::endl;	
return 0;
}
// 1. error
// 1.1 llvm:  c++ -std=c++11 -stdlib=libc++ -Wall cpp2011-14-8-2.cpp
cpp2011-14-8-2.cpp:74:8: error: no template named 'Array'; did you mean 'array'?
void f(Array<dcomplex>& cv, Array<int>& ci) {
       ^~~~~
       array
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/array:121:30: note: 
      'array' declared here
struct _LIBCPP_TYPE_VIS_ONLY array
                             ^
cpp2011-14-8-2.cpp:74:8: error: too few template arguments for class template 'array'
void f(Array<dcomplex>& cv, Array<int>& ci) {
       ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/array:121:30: note: 
      template is declared here
struct _LIBCPP_TYPE_VIS_ONLY array
                             ^
cpp2011-14-8-2.cpp:74:29: error: no template named 'Array'; did you mean 'array'?
void f(Array<dcomplex>& cv, Array<int>& ci) {
                            ^~~~~
                            array
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/array:121:30: note: 
      'array' declared here
struct _LIBCPP_TYPE_VIS_ONLY array
                             ^
cpp2011-14-8-2.cpp:74:29: error: too few template arguments for class template 'array'
void f(Array<dcomplex>& cv, Array<int>& ci) {
                            ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/array:121:30: note: 
      template is declared here
struct _LIBCPP_TYPE_VIS_ONLY array
                             ^
cpp2011-14-8-2.cpp:79:9: error: use of undeclared identifier 'convert'
int i = convert<int>(d); // calls convert<int,double>(double)
        ^
cpp2011-14-8-2.cpp:79:20: error: expected '(' for function-style cast or type construction
int i = convert<int>(d); // calls convert<int,double>(double)
                ~~~^
cpp2011-14-8-2.cpp:80:9: error: use of undeclared identifier 'convert'
int c = convert<char>(d); // calls convert<char,double>(double)
        ^
cpp2011-14-8-2.cpp:80:21: error: expected '(' for function-style cast or type construction
int c = convert<char>(d); // calls convert<char,double>(double)
                ~~~~^
cpp2011-14-8-2.cpp:106:5: error: cannot initialize a variable of type 'int' with an rvalue of type 'void'
int I = f<int>(0);
    ^   ~~~~~~~~~
cpp2011-14-8-2.cpp:110:5: error: cannot initialize a variable of type 'int' with an rvalue of type 'void'
int i = f<int>(0);
    ^   ~~~~~~~~~
cpp2011-14-8-2.cpp:127:10: error: no matching function for call to 'f3'
int i2 = f3<int>(0);
         ^~~~~~~
cpp2011-14-8-2.cpp:126:24: note: candidate template ignored: substitution failure [with T = int]: member pointer refers
      into non-class type 'int'
template <class T> int f3(int T::*);
                       ^         ~
cpp2011-14-8-2.cpp:130:32: error: template argument for non-type template parameter is treated as function type 'T ()'
template <class T> int f4(S<T, T()>*);
                               ^~~
cpp2011-14-8-2.cpp:129:21: note: template parameter is declared here
template <class T, T> struct S {};
                    ^
cpp2011-14-8-2.cpp:132:10: error: no matching function for call to 'f4'
int i0 = f4<X3>(0);
         ^~~~~~
>>13 errors generated.
// 1.2 gcc:	g++-4.9 -std=c++11  -Wall cpp2011-14-8-2.cpp
cpp2011-14-8-2.cpp:74:8: error: variable or field 'f' declared void
 void f(Array<dcomplex>& cv, Array<int>& ci) {
        ^
cpp2011-14-8-2.cpp:74:8: error: 'Array' was not declared in this scope
cpp2011-14-8-2.cpp:74:22: error: expected primary-expression before '>' token
 void f(Array<dcomplex>& cv, Array<int>& ci) {
                      ^
cpp2011-14-8-2.cpp:74:25: error: 'cv' was not declared in this scope
 void f(Array<dcomplex>& cv, Array<int>& ci) {
                         ^
cpp2011-14-8-2.cpp:74:29: error: 'Array' was not declared in this scope
 void f(Array<dcomplex>& cv, Array<int>& ci) {
                             ^
cpp2011-14-8-2.cpp:74:35: error: expected primary-expression before 'int'
 void f(Array<dcomplex>& cv, Array<int>& ci) {
                                   ^
cpp2011-14-8-2.cpp: In function 'void g(double)':
cpp2011-14-8-2.cpp:79:9: error: 'convert' was not declared in this scope
 int i = convert<int>(d); // calls convert<int,double>(double)
         ^
cpp2011-14-8-2.cpp:79:17: error: expected primary-expression before 'int'
 int i = convert<int>(d); // calls convert<int,double>(double)
                 ^
cpp2011-14-8-2.cpp:80:17: error: expected primary-expression before 'char'
 int c = convert<char>(d); // calls convert<char,double>(double)
                 ^
cpp2011-14-8-2.cpp:79:5: warning: unused variable 'i' [-Wunused-variable]
 int i = convert<int>(d); // calls convert<int,double>(double)
     ^
cpp2011-14-8-2.cpp:80:5: warning: unused variable 'c' [-Wunused-variable]
 int c = convert<char>(d); // calls convert<char,double>(double)
     ^
cpp2011-14-8-2.cpp: At global scope:
cpp2011-14-8-2.cpp:106:17: error: void value not ignored as it ought to be
 int I = f<int>(0);
                 ^
cpp2011-14-8-2.cpp:110:17: error: void value not ignored as it ought to be
 int i = f<int>(0);
                 ^
cpp2011-14-8-2.cpp:127:19: error: no matching function for call to 'f3(int)'
 int i2 = f3<int>(0);
                   ^
cpp2011-14-8-2.cpp:127:19: note: candidate is:
cpp2011-14-8-2.cpp:126:24: note: template<class T> int f3(int T::*)
 template <class T> int f3(int T::*);
                        ^
cpp2011-14-8-2.cpp:126:24: note:   template argument deduction/substitution failed:
cpp2011-14-8-2.cpp: In substitution of 'template<class T> int f3(int T::*) [with T = int]':
cpp2011-14-8-2.cpp:127:19:   required from here
cpp2011-14-8-2.cpp:126:24: error: creating pointer to member of non-class type 'int'
cpp2011-14-8-2.cpp:130:35: error: type/value mismatch at argument 2 in template parameter list for 'template<class T, T <anonymous> > struct S'
 template <class T> int f4(S<T, T()>*);
                                   ^
cpp2011-14-8-2.cpp:130:35: error:   expected a constant of type 'T', got 'T()'					
					
