接続数

COUNTER372105FROM 2012/5/16

MISRA-C diary(C言語日誌)

MISRA-C >> Article details

2018/04/15

C++ Templates: The Complete Guide (2nd Edition) 2nd Edition

Tweet ThisSend to Facebook | by kaizen
C++ Templates: The Complete Guide (2nd Edition)  2nd Edition
C++ Templates: The Complete Guide 2nd Edition (English Edition)
David Vandevoorde, Nicolai M Josuttis, Douglas Gregor(2018/04/09)


Code

---ca.sh
#!/bin/bash
while read line
do
  echo "clang++ $line -std=c++2a -Wall"
  clang++ $line -std=c++2a -Wall 
  if [  -e a.out ]; then
  ./a.out
  rm a.out
  fi
done < ./$1
---
---ba
arrays.cpp
cref.cpp
errornovel1.cpp
errornovel2.cpp
foldtraverse.cpp
foreach.cpp
foreachinvoke.cpp
max1.cpp
max2.cpp
max3ref.cpp
max3val.cpp
max4.cpp
message.cpp
move1.cpp
move2.cpp
myfirst.cpp
myfirstmain.cpp
referror1.cpp
referror2.cpp
specialmemtmpl1.cpp
specialmemtmpl3.cpp
stack1test.cpp
stack3test.cpp
stack9test.cpp
stackauto.cpp
stacknontype.cpp
tmplparamref.cpp
varusing.cpp
---

../ca.sh ba > ba.log &> ba.log


../ca.sh br > de.log.txt &>  de.log.txt
---de
adl.cpp
fppm.cpp
funcoverload1.cpp
funcoverload2.cpp
implicit.cpp
inject.cpp
lazy2.cpp
max.cpp
nontmpl1.cpp
nontmpl2.cpp
tmplconstr.cpp
tupleoverload.cpp
variadicoverload.cpp
---


sm.log.txt

---arrays.cpp

#include "arrays.hpp"


template<typename T1, typename T2, typename T3>

void foo(int a1[7], int a2[],    // pointers by language rules

         int (&a3)[42],          // reference to array of known bound

         int (&x0)[],            // reference to array of unknown bound

         T1 x1,                  // passing by value decays

         T2& x2, T3&& x3)        // passing by reference

{

  MyClass<decltype(a1)>::print();     // uses MyClass<T*>

  MyClass<decltype(a2)>::print();     // uses MyClass<T*>

  MyClass<decltype(a3)>::print();     // uses MyClass<T(\&)[SZ]>

  MyClass<decltype(x0)>::print();     // uses MyClass<T(\&)[]>

  MyClass<decltype(x1)>::print();     // uses MyClass<T*>

  MyClass<decltype(x2)>::print();     // uses MyClass<T(\&)[]>

  MyClass<decltype(x3)>::print();     // uses MyClass<T(\&)[]>

}


int main()

{

  int a[42];

  MyClass<decltype(a)>::print();      // uses MyClass<T[SZ]>


  extern int x[];                     // forward declare array

  MyClass<decltype(x)>::print();      // uses MyClass<T[]>


  foo(a, a, a, x, x, x, x);

}


int x[] = {0, 8, 15};                 // define forward-declared array


---arrays.hpp

#include <iostream>


template<typename T>

struct MyClass;             // primary template


template<typename T, std::size_t SZ>

struct MyClass<T[SZ]>       // partial specialization for arrays of known bounds

{

  static void print() { std::cout << "print() for T[" << SZ << "]\n"; }

};


template<typename T, std::size_t SZ>

struct MyClass<T(&)[SZ]>    // partial spec. for references to arrays of known bounds

{

  static void print() { std::cout << "print() for T(&)[" << SZ << "]\n"; }

};


template<typename T>

struct MyClass<T[]>         // partial specialization for arrays of unknown bounds

{

  static void print() { std::cout << "print() for T[]\n"; }

};


template<typename T>

struct MyClass<T(&)[]>      // partial spec. for references to arrays of unknown bounds

{

  static void print() { std::cout << "print() for T(&)[]\n"; }

};


template<typename T>

struct MyClass<T*>          // partial specialization for pointers

{

  static void print() { std::cout << "print() for T*\n"; }

};

---

 ./cpgl.sh arrays

$ clang++ arrays.cpp

print() for T[42]

print() for T[]

print() for T*

print() for T*

print() for T(&)[42]

print() for T(&)[]

print() for T*

print() for T(&)[]

print() for T(&)[]


$ g++-7 arrays.cpp

arrays.cpp:8:25:error: parameter 'x0' includes reference to array of unknown bound 'int []'

          T2& x2, T3&& x3)        // passing by reference

                         ^

---
Clang++で実行。
g++でコンパイルエラー



https://www.boost.org
22:49 | Impressed! | Voted(0) | Comment(0)