0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

13.9.4 Explicit specialization [temp.expl.spec] C++N4910:2022 (236) p415.cpp

Last updated at Posted at 2022-07-06

はじめに(Introduction)

N4910 Working Draft, Standard for Programming Language C++

n4910は、ISO/IEC JTC1 SC22 WG21の作業原案(Working Draft)です。
公式のISO/IEC 14882原本ではありません。
ISO/IEC JTC1 SC22 WG21では、可能な限り作業文書を公開し、幅広い意見を求めています。
一連の記事はコード断片をコンパイルできる形にする方法を検討してコンパイル、リンク、実行して、規格案の原文と処理系(g++, Clang++)との違いを確認し、技術内容を検討し、ISO/IEC JTC1 SC22 WG21にフィードバックするために用います。
また、CERT C++, MISRA C++等のコーディング標準のコード断片をコンパイルする際の参考にさせていただこうと考えています。CERT C++, MISRA C++が標準化の動きとの時間的なずれがあれば確認できれば幸いです。また、boostライブラリとの関連、Linux OS, TOPPERSカーネル、g++(GCC), clang++(LLVM)との関係も調査中です。
何か、抜け漏れ、耳より情報がありましたらおしらせくださると幸いです。

<この項は書きかけです。順次追記します。>

背景(back ground)

C/C++でコンパイルエラーが出ると、途方にくれることがしばしばあります。
何回かに1回は、該当するエラーが検索できます。
ただ、条件が違っていて、そこでの修正方法では目的を達成しないこともしばしばです。いろいろな条件のコンパイルエラーとその対応方法について、広く記録することによって、いつか同じエラーに遭遇した時にやくに立つことを目指しています。

この半年の間で、三度、自分のネットでの記録に助けられたことがあります。
また過去に解決できなかった記録を10種類以上、最近になって解決できたことがあります。それは、主に次の3つの情報に基づいています。

https://stackoverflow.com
https://cpprefjp.github.io
http://ja.cppreference.com/

また
https://researchmap.jp/joub9b3my-1797580/#_1797580
に記載したサイトのお世話になっています。

作業方針(sequence)

Clang++では-std=c++03, C++2bの2種類
g++では-std=c++03, c++2bの2種類
でコンパイルし、

1)コンパイルエラーを収集する。
2)コンパイルエラーをなくす方法を検討する。
コンパイルエラーになる例を示すだけが目的のコードは、コンパイルエラーをなくすのではなく、コンパイルエラーの種類を収集するだけにする。
文法を示すのが目的のコード場合に、コンパイルエラーをなくすのに手間がかかる場合は、順次作業します。
3)リンクエラーをなくす方法を検討する。
文法を示すのが目的のコード場合に、リンクエラーをなくすのに手間がかかる場合は、順次作業します。
4)意味のある出力を作る。
コンパイル、リンクが通っても、意味のある出力を示そうとすると、コンパイル・リンクエラーが出て収拾できそうにない場合がある。順次作業します。

1)だけのものから4)まで進んだものと色々ある状態です。一歩でも前に進むご助言をお待ちしています。「検討事項」の欄に現状を記録するようにしています。

C++N4910:2022 Standard Working Draft on ISO/IEC 14882(0) sample code compile list

C++N4741, 2018 Standard Working Draft on ISO/IEC 14882 sample code compile list

C++N4606, 2016符号断片編纂一覧(example code compile list)

C++N4606, 2016 Working Draft 2016, ISO/IEC 14882, C++ standard(1) Example code compile list
https://qiita.com/kaizen_nagoya/items/df5d62c35bd6ed1c3d43/

C++N3242, 2011 sample code compile list on clang++ and g++

編纂器(Compiler)

clang++ --version

Debian clang version 14.0.5-++20220610033153+c12386ae247c-1~exp1~20220610153237.151
Target: x86_64-pc-linux-gnu, Thread model: posix, InstalledDir: /usr/bin

g++- --version

g++ (GCC) 12.1.0 Copyright (C) 2022 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.

13.9.4 Explicit specialization [temp.expl.spec] C++N4910:2022 (236) p415.cpp

算譜(source code)

p415.cpp
// C++N4910 Committee Draft, Standard for Programming Language C++
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/n4910.pdf
const char * n4910 = "13.9.4 Explicit specialization [temp.expl.spec] C++N4910:2022 (236) p415.cpp";
// Debian clang version 14.0.5-++20220610033153+c12386ae247c-
// g++ (GCC) 12.1.0 Copyright (C) 2022 Free Software Foundation, Inc.
// Edited by Dr. Ogawa Kiyoshi. Compile procedure and results record.
// C++N4910:2022 Standard Working Draft on ISO/IEC 14882(0) sample code compile list
// https://qiita.com/kaizen_nagoya/items/fc957ddddd402004bb91

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <coroutine>
#include <vector>
#include <complex>
#include <map>
#include <atomic>
#include <unordered_map>
#include <typeinfo>

using namespace std;

// Example 1
template<class T> class stream;
template<> class stream<char> { /* ... */
};
template<class T> class Array { /* ... */
};
template<class T> void sort(Array<T>& v) { /* ... */ }
template<> void sort<char*>(Array<char*>&);
// Given these declarations, stream<char> will be used as the definition of streams of chars; other streams will be handled by class template specializations instantiated from the class template. Similarly, sort<char*> will be used as the sort function for arguments of type Array<char*>; other Array types will be sorted by functions generated from the template.
// [Example 2:
template<> class X<int> { /* ... */
};
template<class T> class X;
template<> class X<char*> { /* ... */
};
// error: X not a template
// OK, X is a template
// [Example 3:
template<class T> struct A {
    struct B { };
    template<class U> struct C { };
};
template<> struct A<int> {
    void f(int);
};
}
void A<int>::f(int) { /* ... */ }
template<> struct A<char>::B {
    void f();
};
// template<> also not used when defining a member of an explicitly specialized member class void A<char>::B::f() { /* ... */ }
template<> template<class U> struct A<char>::C {
    void f();
};
// template<> is used when defining a member of an explicitly specialized member class template // specialized as a class template
template<>
template<class U> void A<char>::C<U>::f() { /* ... */ }
void h() {
    A<int> a;
    a.f(16); // A<int>::f must be defined somewhere
}
// template<> not used for a member of an explicitly specialized class template
template<> struct A<short>::B {
    void f();
};
template<> void A<short>::B::f() { /* ... */ }
template<> template<class U> struct A<short>::C {
    void f();
};
template<class U> void A<short>::C<U>::f() { /* ... */ }
// error: template<> not permitted
// error: template<> required
// [Example 4:
class String { };
template<class T> class Array { /* ... */
};
template<class T> void sort(Array<T>& v) { /* ... */ }
void f(Array<String>& v) {
}
sort(v);
// use primary template sort(Array<T>&), T is String
// error: specialization after use of primary template // OK, sort<char*> not yet used
void sort<String>(Array<String>& v);
void sort<>(Array<char*>& v);
template<>
template<>
template<class T> struct A {
    enum E : T;
    enum class S : T;
};
template<> enum A<int>::E : int { eint };
// OK
template<> enum class A<int>::S : int { sint };
template<class T> enum A<T>::E : T { eT };
template<class T> enum class A<T>::S : T { sT };
template<> enum A<char>::E : char { echar };
// OK
// error: A<char>::E was instantiated
// when A<char> was instantiated template<> enum class A<char>::S : char { schar }; // OK
// [Example 5:
template<class T> class X;
template<> class X<int>;
// X is a class template
// OK, pointer to declared class X<int>
// error: object of incomplete class X<int>
// [Example 6:
template<class T> class Array { /* ... */
};
template<class T> void sort(Array<T>& v);
// explicit specialization for sort(Array<int>&) // with deduced template-argument of type int template<> void sort(Array<int>&);
X<int>* p;
X<int> x;
// [Example 7:
template<class T> void f(T) { /* ... */ }
template<class T> inline T g(T) { /* ... */ }
template<> inline void f<>(int) { /* ... */ }
template<> int g<>(int) { /* ... */ }
// OK, inline
// OK, not inline
// [Note 3: The definition of a static data member of a template for which default-initialization is desired can use functional cast notation (7.6.1.4):
template<> X Q<int>::x;
template<> X Q<int>::x ();
template<> X Q<int>::x = X();
// declaration
// error: declares a function
// definition
// [Example 8:
template<class T> struct A {
    void f(T);
    template<class X1> void g1(T, X1);
    template<class X2> void g2(T, X2);
    void h(T) { }
};
// specialization
template<> void A<int>::f(int);
// out of class member template definition
template<class T> template<class X1> void A<T>::g1(T, X1) { }
// member template specialization
template<> template<class X1> void A<int>::g1(int, X1);
// member template specialization
template<> template<>
void A<int>::g1(int, char); // X1 deduced as char
template<> template<>
void A<int>::g2<char>(int, char); // X2 specified as char
// member specialization even if defined in class definition
template<> void A<int>::h(int) { }
// [Example 9:
template<class T1> class A {
    template<class T2> class B {
        void mf();
    };
};
template<> template<> class A<int>::B<double>;
template<> template<> void A<char>::B<char>::mf();
// [Example 10:
template <class T1> class A {
    template<class T2> class B {
        template<class T3> void mf1(T3);
        void mf2();
    };
};
template <> template <class X>
class A<int>::B {
    template <class T> void mf1(T);
};
template <> template <> template<class T>
void A<int>::B<double>::mf1(T t) { }
template <class Y> template <>
void A<Y>::B<double>::mf2() { } // error: B<double> is specialized but // its enclosing class template A is not
int main() {
    cout  <<  n4910 << endl;
    return EXIT_SUCCESS;
}

Script

clgc.sh
#!/bin/sh
rm $1l
rm $1g
echo "$ clang++ $1.cpp -std=03 -o $1l -I. -Wall" 
clang++ $1.cpp -std=c++03 -o $1l -I. -Wall
if [  -e $1l ]; then
./$1l 
fi
rm $1l
echo "$ clang++ $1.cpp -std=2b -o $1l -I. -Wall"
clang++ $1.cpp -std=c++2b -o $1l -I. -Wall
if [  -e $1l ]; then
./$1l
fi
echo "\r"
echo "$ g++ $1.cpp -std=03 -o $1g -I. -Wall"
g++ $1.cpp -std=c++03 -o $1g -I. -Wall
if [ -e $1g ]; then
./$1g
fi
rm $1g
echo "\r"
echo "$ g++ $1.cpp -std=2b -o $1g -I. -Wall"
g++ $1.cpp -std=c++2b -o $1g -I. -Wall
if [ -e $1g ]; then
./$1g 
fi

編纂・実行結果(compile and go)

bash
# ./clgc.sh p415
rm: cannot remove 'p415l': No such file or directory
rm: cannot remove 'p415g': No such file or directory
$ clang++ p415.cpp -std=c++03 -o p415l -I. -Wall
In file included from p415.cpp:19:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/atomic:38:
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/c++0x_warning.h:32:2: error: This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
#error This file requires compiler and library support \
 ^
p415.cpp:33:18: error: explicit specialization of undeclared template class 'X'
template<> class X<int> { /* ... */ };
                 ^~~~~~
p415.cpp:34:27: error: redefinition of 'X' as different kind of symbol
  template<class T> class X;
                          ^
p415.cpp:33:18: note: previous definition is here
template<> class X<int> { /* ... */ };
                 ^
p415.cpp:35:20: error: explicit specialization of non-template class 'X'
  template<> class X<char*> { /* ... */ };
                   ^~~~~~~~
p415.cpp:35:20: error: redefinition of 'X'
p415.cpp:33:18: note: previous definition is here
template<> class X<int> { /* ... */ };
                 ^
p415.cpp:46:1: error: extraneous closing brace ('}')
}
^
p415.cpp:66:32: error: no function template matches function template specialization 'f'
  template<> void A<short>::B::f() { /* ... */ }
                               ^
p415.cpp:70:3: error: template parameter list matching the non-templated nested type 'A<short>' should be empty ('template<>')
  template<class U> void A<short>::C<U>::f() { /* ... */ }
  ^       ~~~~~~~~~      ~~~~~~~~
p415.cpp:75:30: error: redefinition of 'Array'
     template<class T> class Array { /* ... */ };
                             ^
p415.cpp:28:27: note: previous definition is here
  template<class T> class Array { /* ... */ };
                          ^
p415.cpp:76:29: error: redefinition of 'sort'
     template<class T> void sort(Array<T>& v) { /* ... */ }
                            ^
p415.cpp:29:26: note: previous definition is here
  template<class T> void sort(Array<T>& v) { /* ... */ }
                         ^
p415.cpp:79:1: error: unknown type name 'sort'; did you mean 'short'?
sort(v);
^~~~
short
p415.cpp:82:6: error: template specialization requires 'template<>'
void sort<String>(Array<String>& v);
     ^   ~~~~~~~~
template<> 
p415.cpp:83:6: error: template specialization requires 'template<>'
void sort<>(Array<char*>& v);
     ^   ~~
template<> 
p415.cpp:84:1: warning: extraneous template parameter list in template specialization
template<>
^~~~~~~~~~
p415.cpp:86:26: error: redefinition of 'A'
template<class T> struct A {
                         ^
p415.cpp:39:26: note: previous definition is here
template<class T> struct A {
                         ^
p415.cpp:90:33: error: enumeration cannot be a template
template<> enum A<int>::E : int { eint };
                                ^
p415.cpp:90:12: error: declaration does not declare anything
template<> enum A<int>::E : int { eint };
           ^
p415.cpp:92:17: warning: scoped enumerations are a C++11 extension [-Wc++11-extensions]
template<> enum class A<int>::S : int { sint };
                ^
p415.cpp:92:39: error: enumeration cannot be a template
template<> enum class A<int>::S : int { sint };
                                      ^
p415.cpp:92:12: error: declaration does not declare anything
template<> enum class A<int>::S : int { sint };
           ^
p415.cpp:93:36: error: enumeration cannot be a template
template<class T> enum A<T>::E : T { eT };
                                   ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
2 warnings and 20 errors generated.
rm: cannot remove 'p415l': No such file or directory
$ clang++ p415.cpp -std=c++2b -o p415l -I. -Wall
p415.cpp:33:18: error: explicit specialization of undeclared template class 'X'
template<> class X<int> { /* ... */ };
                 ^~~~~~
p415.cpp:34:27: error: redefinition of 'X' as different kind of symbol
  template<class T> class X;
                          ^
p415.cpp:33:18: note: previous definition is here
template<> class X<int> { /* ... */ };
                 ^
p415.cpp:35:20: error: explicit specialization of non-template class 'X'
  template<> class X<char*> { /* ... */ };
                   ^~~~~~~~
p415.cpp:35:20: error: redefinition of 'X'
p415.cpp:33:18: note: previous definition is here
template<> class X<int> { /* ... */ };
                 ^
p415.cpp:46:1: error: extraneous closing brace ('}')
}
^
p415.cpp:66:32: error: no function template matches function template specialization 'f'
  template<> void A<short>::B::f() { /* ... */ }
                               ^
p415.cpp:70:3: error: template parameter list matching the non-templated nested type 'A<short>' should be empty ('template<>')
  template<class U> void A<short>::C<U>::f() { /* ... */ }
  ^       ~~~~~~~~~      ~~~~~~~~
p415.cpp:75:30: error: redefinition of 'Array'
     template<class T> class Array { /* ... */ };
                             ^
p415.cpp:28:27: note: previous definition is here
  template<class T> class Array { /* ... */ };
                          ^
p415.cpp:76:29: error: redefinition of 'sort'
     template<class T> void sort(Array<T>& v) { /* ... */ }
                            ^
p415.cpp:29:26: note: previous definition is here
  template<class T> void sort(Array<T>& v) { /* ... */ }
                         ^
p415.cpp:79:1: error: unknown type name 'sort'; did you mean 'short'?
sort(v);
^~~~
short
p415.cpp:82:6: error: template specialization requires 'template<>'
void sort<String>(Array<String>& v);
     ^   ~~~~~~~~
template<> 
p415.cpp:83:6: error: template specialization requires 'template<>'
void sort<>(Array<char*>& v);
     ^   ~~
template<> 
p415.cpp:84:1: warning: extraneous template parameter list in template specialization
template<>
^~~~~~~~~~
p415.cpp:86:26: error: redefinition of 'A'
template<class T> struct A {
                         ^
p415.cpp:39:26: note: previous definition is here
template<class T> struct A {
                         ^
p415.cpp:90:12: error: enumeration cannot be a template
template<> enum A<int>::E : int { eint };
           ^
p415.cpp:92:12: error: enumeration cannot be a template
template<> enum class A<int>::S : int { sint };
           ^
p415.cpp:93:30: error: no enum named 'E' in 'A<T>'
template<class T> enum A<T>::E : T { eT };
                       ~~~~~~^
p415.cpp:94:36: error: no enum named 'S' in 'A<T>'
template<class T> enum class A<T>::S : T { sT };
                             ~~~~~~^
p415.cpp:95:26: error: no enum named 'E' in 'A<char>'
template<> enum A<char>::E : char { echar };
                ~~~~~~~~~^
p415.cpp:100:25: error: redefinition of 'X' as different kind of symbol
template<class T> class X;
                        ^
p415.cpp:33:18: note: previous definition is here
template<> class X<int> { /* ... */ };
                 ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
1 warning and 20 errors generated.

$ g++ p415.cpp -std=c++03 -o p415g -I. -Wall
In file included from /usr/local/include/c++/12.1.0/atomic:38,
                 from p415.cpp:19:
/usr/local/include/c++/12.1.0/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
   32 | #error This file requires compiler and library support \
      |  ^~~~~
p415.cpp:33:18: error: 'X' is not a class template
   33 | template<> class X<int> { /* ... */ };
      |                  ^
p415.cpp:33:25: error: explicit specialization of non-template 'X'
   33 | template<> class X<int> { /* ... */ };
      |                         ^
p415.cpp:34:27: error: 'X' is not a template
   34 |   template<class T> class X;
      |                           ^
p415.cpp:33:18: note: previous declaration here
   33 | template<> class X<int> { /* ... */ };
      |                  ^
p415.cpp:35:20: error: 'X' is not a class template
   35 |   template<> class X<char*> { /* ... */ };
      |                    ^
p415.cpp:35:27: error: 'X' is not a template
   35 |   template<> class X<char*> { /* ... */ };
      |                           ^
p415.cpp:33:18: note: previous declaration here
   33 | template<> class X<int> { /* ... */ };
      |                  ^
p415.cpp:46:1: error: expected declaration before '}' token
   46 | }
      | ^
p415.cpp:66:19: error: template-id 'f<>' for 'void A<short int>::B::f()' does not match any template declaration
   66 |   template<> void A<short>::B::f() { /* ... */ }
      |                   ^~~~~~~~
p415.cpp:64:10: note: candidate is: 'void A<short int>::B::f()'
   64 |     void f();
      |          ^
p415.cpp:70:26: error: specializing member 'A<short int>::C<U>::f' requires 'template<>' syntax
   70 |   template<class U> void A<short>::C<U>::f() { /* ... */ }
      |                          ^~~~~~~~
p415.cpp:75:30: error: redefinition of 'class Array<T>'
   75 |      template<class T> class Array { /* ... */ };
      |                              ^~~~~
p415.cpp:28:27: note: previous definition of 'class Array<T>'
   28 |   template<class T> class Array { /* ... */ };
      |                           ^~~~~
p415.cpp:76:29: error: redefinition of 'template<class T> void sort(Array<T>&)'
   76 |      template<class T> void sort(Array<T>& v) { /* ... */ }
      |                             ^~~~
p415.cpp:29:26: note: 'template<class T> void sort(Array<T>&)' previously declared here
   29 |   template<class T> void sort(Array<T>& v) { /* ... */ }
      |                          ^~~~
p415.cpp:79:5: error: expected constructor, destructor, or type conversion before '(' token
   79 | sort(v);
      |     ^
p415.cpp:82:6: error: specializing member '::sort<String>' requires 'template<>' syntax
   82 | void sort<String>(Array<String>& v);
      |      ^~~~~~~~~~~~
p415.cpp:83:6: error: specializing member '::sort<>' requires 'template<>' syntax
   83 | void sort<>(Array<char*>& v);
      |      ^~~~~~
p415.cpp:86:26: error: too many template-parameter-lists
   86 | template<class T> struct A {
      |                          ^
p415.cpp:90:17: error: 'E' does not name an enumeration in 'A<int>'
   90 | template<> enum A<int>::E : int { eint };
      |                 ^~~~~~
p415.cpp:90:29: warning: scoped enums only available with '-std=c++11' or '-std=gnu++11' [-Wc++11-extensions]
   90 | template<> enum A<int>::E : int { eint };
      |                             ^~~
p415.cpp:90:40: error: explicit specialization of non-template 'E'
   90 | template<> enum A<int>::E : int { eint };
      |                                        ^
p415.cpp:92:12: warning: scoped enums only available with '-std=c++11' or '-std=gnu++11' [-Wc++11-extensions]
   92 | template<> enum class A<int>::S : int { sint };
      |            ^~~~
p415.cpp:92:23: error: 'S' does not name an enumeration in 'A<int>'
   92 | template<> enum class A<int>::S : int { sint };
      |                       ^~~~~~
p415.cpp:92:35: warning: scoped enums only available with '-std=c++11' or '-std=gnu++11' [-Wc++11-extensions]
   92 | template<> enum class A<int>::S : int { sint };
      |                                   ^~~
p415.cpp:92:46: error: explicit specialization of non-template 'S'
   92 | template<> enum class A<int>::S : int { sint };
      |                                              ^
p415.cpp:93:34: warning: scoped enums only available with '-std=c++11' or '-std=gnu++11' [-Wc++11-extensions]
   93 | template<class T> enum A<T>::E : T { eT };
      |                                  ^
p415.cpp:94:19: warning: scoped enums only available with '-std=c++11' or '-std=gnu++11' [-Wc++11-extensions]
   94 | template<class T> enum class A<T>::S : T { sT };
      |                   ^~~~
p415.cpp:94:40: warning: scoped enums only available with '-std=c++11' or '-std=gnu++11' [-Wc++11-extensions]
   94 | template<class T> enum class A<T>::S : T { sT };
      |                                        ^
p415.cpp:95:17: error: 'E' does not name an enumeration in 'A<char>'
   95 | template<> enum A<char>::E : char { echar };
      |                 ^~~~~~~
p415.cpp:95:30: warning: scoped enums only available with '-std=c++11' or '-std=gnu++11' [-Wc++11-extensions]
   95 | template<> enum A<char>::E : char { echar };
      |                              ^~~~
p415.cpp:95:30: error: different underlying type in enum 'enum E'
p415.cpp:90:17: note: previous definition here
   90 | template<> enum A<int>::E : int { eint };
      |                 ^~~~~~
p415.cpp:95:17: error: multiple definition of 'enum E'
   95 | template<> enum A<char>::E : char { echar };
      |                 ^~~~~~~
p415.cpp:90:17: note: previous definition here
   90 | template<> enum A<int>::E : int { eint };
      |                 ^~~~~~
p415.cpp:100:25: error: 'X' is not a template
  100 | template<class T> class X;
      |                         ^
p415.cpp:33:18: note: previous declaration here
   33 | template<> class X<int> { /* ... */ };
      |                  ^
p415.cpp:101:20: error: 'X' is not a class template
  101 |   template<> class X<int>;
      |                    ^
p415.cpp:101:20: error: 'X' is not a template
p415.cpp:33:18: note: previous declaration here
   33 | template<> class X<int> { /* ... */ };
      |                  ^
p415.cpp:106:30: error: redefinition of 'class Array<T>'
  106 |      template<class T> class Array { /* ... */ };
      |                              ^~~~~
p415.cpp:28:27: note: previous definition of 'class Array<T>'
   28 |   template<class T> class Array { /* ... */ };
      |                           ^~~~~
p415.cpp:109:1: error: 'X' is not a template
  109 | X<int>* p;
      | ^
p415.cpp:110:1: error: 'X' is not a template
  110 | X<int> x;
      | ^
p415.cpp: In function 'T g(T) [with T = int]':
p415.cpp:115:39: warning: no return statement in function returning non-void [-Wreturn-type]
  115 |   template<> int g<>(int) { /* ... */ }
      |                                       ^
p415.cpp: At global scope:
p415.cpp:119:17: error: expected initializer before '<' token
  119 |   template<> X Q<int>::x;
      |                 ^
p415.cpp:120:17: error: expected initializer before '<' token
  120 |   template<> X Q<int>::x ();
      |                 ^
p415.cpp:121:17: error: expected initializer before '<' token
  121 |   template<> X Q<int>::x = X();
      |                 ^
p415.cpp:126:31: error: redefinition of 'struct A<T>'
  126 |      template<class T> struct A {
      |                               ^
p415.cpp:39:26: note: previous definition of 'struct A<T>'
   39 | template<class T> struct A {
      |                          ^
p415.cpp:133:22: error: template-id 'f<>' for 'void A<int>::f(int)' does not match any template declaration
  133 |      template<> void A<int>::f(int);
      |                      ^~~~~~
p415.cpp:47:6: note: candidate is: 'void A<int>::f(int)'
   47 | void A<int>::f(int) { /* ... */ }
      |      ^~~~~~
p415.cpp:135:48: error: no declaration matches 'void A<T>::g1(T, X1)'
  135 |      template<class T> template<class X1> void A<T>::g1(T, X1) { }
      |                                                ^~~~
p415.cpp:135:48: note: no functions named 'void A<T>::g1(T, X1)'
p415.cpp:39:26: note: 'struct A<T>' defined here
   39 | template<class T> struct A {
      |                          ^
p415.cpp:137:41: error: too many template-parameter-lists
  137 |      template<> template<class X1> void A<int>::g1(int, X1);
      |                                         ^~~~~~
p415.cpp:140:6: error: too many template-parameter-lists
  140 | void A<int>::g1(int, char); // X1 deduced as char
      |      ^~~~~~
p415.cpp:142:6: error: too many template-parameter-lists
  142 | void A<int>::g2<char>(int, char); // X2 specified as char
      |      ^~~~~~
p415.cpp:144:35: error: no member function 'h' declared in 'A<int>'
  144 |      template<> void A<int>::h(int) { }
      |                                   ^
p415.cpp:146:31: error: redefinition of 'class A<T>'
  146 |      template<class T1> class A {
      |                               ^
p415.cpp:39:26: note: previous definition of 'class A<T>'
   39 | template<class T> struct A {
      |                          ^
p415.cpp:150:42: error: 'B' is not a class template
  150 |      template<> template<> class A<int>::B<double>;
      |                                          ^
p415.cpp:150:42: error: 'B' in 'struct A<int>' does not name a type
p415.cpp:151:43: error: expected initializer before '<' token
  151 |      template<> template<> void A<char>::B<char>::mf();
      |                                           ^
p415.cpp:153:32: error: redefinition of 'class A<T>'
  153 |      template <class T1> class A {
      |                                ^
p415.cpp:39:26: note: previous definition of 'class A<T>'
   39 | template<class T> struct A {
      |                          ^
p415.cpp:160:24: error: qualified name does not name a class before '{' token
  160 |        class A<int>::B {
      |                        ^
p415.cpp:164:13: error: too many template-parameter-lists
  164 |        void A<int>::B<double>::mf1(T t) { }
      |             ^~~~~~
p415.cpp:165:35: error: invalid explicit specialization before '>' token
  165 |      template <class Y> template <>
      |                                   ^
p415.cpp:165:35: error: enclosing class templates are not explicitly specialized
p415.cpp:166:13: error: expected initializer before '<' token
  166 | void A<Y>::B<double>::mf2() { } // error: B<double> is specialized but // its enclosing class template A is not
      |             ^
rm: cannot remove 'p415g': No such file or directory

$ g++ p415.cpp -std=c++2b -o p415g -I. -Wall
p415.cpp:33:18: error: 'X' is not a class template
   33 | template<> class X<int> { /* ... */ };
      |                  ^
p415.cpp:33:25: error: explicit specialization of non-template 'X'
   33 | template<> class X<int> { /* ... */ };
      |                         ^
p415.cpp:34:27: error: 'X' is not a template
   34 |   template<class T> class X;
      |                           ^
p415.cpp:33:18: note: previous declaration here
   33 | template<> class X<int> { /* ... */ };
      |                  ^
p415.cpp:35:20: error: 'X' is not a class template
   35 |   template<> class X<char*> { /* ... */ };
      |                    ^
p415.cpp:35:27: error: 'X' is not a template
   35 |   template<> class X<char*> { /* ... */ };
      |                           ^
p415.cpp:33:18: note: previous declaration here
   33 | template<> class X<int> { /* ... */ };
      |                  ^
p415.cpp:46:1: error: expected declaration before '}' token
   46 | }
      | ^
p415.cpp:66:19: error: template-id 'f<>' for 'void A<short int>::B::f()' does not match any template declaration
   66 |   template<> void A<short>::B::f() { /* ... */ }
      |                   ^~~~~~~~
p415.cpp:64:10: note: candidate is: 'void A<short int>::B::f()'
   64 |     void f();
      |          ^
p415.cpp:70:26: error: specializing member 'A<short int>::C<U>::f' requires 'template<>' syntax
   70 |   template<class U> void A<short>::C<U>::f() { /* ... */ }
      |                          ^~~~~~~~
p415.cpp:75:30: error: redefinition of 'class Array<T>'
   75 |      template<class T> class Array { /* ... */ };
      |                              ^~~~~
p415.cpp:28:27: note: previous definition of 'class Array<T>'
   28 |   template<class T> class Array { /* ... */ };
      |                           ^~~~~
p415.cpp:76:29: error: redefinition of 'template<class T> void sort(Array<T>&)'
   76 |      template<class T> void sort(Array<T>& v) { /* ... */ }
      |                             ^~~~
p415.cpp:29:26: note: 'template<class T> void sort(Array<T>&)' previously declared here
   29 |   template<class T> void sort(Array<T>& v) { /* ... */ }
      |                          ^~~~
p415.cpp:79:5: error: expected constructor, destructor, or type conversion before '(' token
   79 | sort(v);
      |     ^
p415.cpp:82:6: error: specializing member '::sort<String>' requires 'template<>' syntax
   82 | void sort<String>(Array<String>& v);
      |      ^~~~~~~~~~~~
p415.cpp:83:6: error: specializing member '::sort<>' requires 'template<>' syntax
   83 | void sort<>(Array<char*>& v);
      |      ^~~~~~
p415.cpp:86:26: error: too many template-parameter-lists
   86 | template<class T> struct A {
      |                          ^
p415.cpp:90:17: error: 'E' does not name an enumeration in 'A<int>'
   90 | template<> enum A<int>::E : int { eint };
      |                 ^~~~~~
p415.cpp:90:40: error: explicit specialization of non-template 'E'
   90 | template<> enum A<int>::E : int { eint };
      |                                        ^
p415.cpp:92:23: error: 'S' does not name an enumeration in 'A<int>'
   92 | template<> enum class A<int>::S : int { sint };
      |                       ^~~~~~
p415.cpp:92:46: error: explicit specialization of non-template 'S'
   92 | template<> enum class A<int>::S : int { sint };
      |                                              ^
p415.cpp:95:17: error: 'E' does not name an enumeration in 'A<char>'
   95 | template<> enum A<char>::E : char { echar };
      |                 ^~~~~~~
p415.cpp:95:30: error: different underlying type in enum 'enum E'
   95 | template<> enum A<char>::E : char { echar };
      |                              ^~~~
p415.cpp:90:17: note: previous definition here
   90 | template<> enum A<int>::E : int { eint };
      |                 ^~~~~~
p415.cpp:95:43: error: explicit specialization of non-template 'E'
   95 | template<> enum A<char>::E : char { echar };
      |                                           ^
p415.cpp:100:25: error: 'X' is not a template
  100 | template<class T> class X;
      |                         ^
p415.cpp:33:18: note: previous declaration here
   33 | template<> class X<int> { /* ... */ };
      |                  ^
p415.cpp:101:20: error: 'X' is not a class template
  101 |   template<> class X<int>;
      |                    ^
p415.cpp:101:20: error: 'X' is not a template
p415.cpp:33:18: note: previous declaration here
   33 | template<> class X<int> { /* ... */ };
      |                  ^
p415.cpp:106:30: error: redefinition of 'class Array<T>'
  106 |      template<class T> class Array { /* ... */ };
      |                              ^~~~~
p415.cpp:28:27: note: previous definition of 'class Array<T>'
   28 |   template<class T> class Array { /* ... */ };
      |                           ^~~~~
p415.cpp:109:1: error: 'X' is not a template
  109 | X<int>* p;
      | ^
p415.cpp:110:1: error: 'X' is not a template
  110 | X<int> x;
      | ^
p415.cpp: In function 'T g(T) [with T = int]':
p415.cpp:115:39: warning: no return statement in function returning non-void [-Wreturn-type]
  115 |   template<> int g<>(int) { /* ... */ }
      |                                       ^
p415.cpp: At global scope:
p415.cpp:119:17: error: expected initializer before '<' token
  119 |   template<> X Q<int>::x;
      |                 ^
p415.cpp:120:17: error: expected initializer before '<' token
  120 |   template<> X Q<int>::x ();
      |                 ^
p415.cpp:121:17: error: expected initializer before '<' token
  121 |   template<> X Q<int>::x = X();
      |                 ^
p415.cpp:126:31: error: redefinition of 'struct A<T>'
  126 |      template<class T> struct A {
      |                               ^
p415.cpp:39:26: note: previous definition of 'struct A<T>'
   39 | template<class T> struct A {
      |                          ^
p415.cpp:133:22: error: template-id 'f<>' for 'void A<int>::f(int)' does not match any template declaration
  133 |      template<> void A<int>::f(int);
      |                      ^~~~~~
p415.cpp:47:6: note: candidate is: 'void A<int>::f(int)'
   47 | void A<int>::f(int) { /* ... */ }
      |      ^~~~~~
p415.cpp:135:48: error: no declaration matches 'void A<T>::g1(T, X1)'
  135 |      template<class T> template<class X1> void A<T>::g1(T, X1) { }
      |                                                ^~~~
p415.cpp:135:48: note: no functions named 'void A<T>::g1(T, X1)'
p415.cpp:39:26: note: 'struct A<T>' defined here
   39 | template<class T> struct A {
      |                          ^
p415.cpp:137:41: error: too many template-parameter-lists
  137 |      template<> template<class X1> void A<int>::g1(int, X1);
      |                                         ^~~~~~
p415.cpp:140:6: error: too many template-parameter-lists
  140 | void A<int>::g1(int, char); // X1 deduced as char
      |      ^~~~~~
p415.cpp:142:6: error: too many template-parameter-lists
  142 | void A<int>::g2<char>(int, char); // X2 specified as char
      |      ^~~~~~
p415.cpp:144:35: error: no member function 'h' declared in 'A<int>'
  144 |      template<> void A<int>::h(int) { }
      |                                   ^
p415.cpp:146:31: error: redefinition of 'class A<T>'
  146 |      template<class T1> class A {
      |                               ^
p415.cpp:39:26: note: previous definition of 'class A<T>'
   39 | template<class T> struct A {
      |                          ^
p415.cpp:150:42: error: 'B' is not a class template
  150 |      template<> template<> class A<int>::B<double>;
      |                                          ^
p415.cpp:150:42: error: 'B' in 'struct A<int>' does not name a type
p415.cpp:151:43: error: expected initializer before '<' token
  151 |      template<> template<> void A<char>::B<char>::mf();
      |                                           ^
p415.cpp:153:32: error: redefinition of 'class A<T>'
  153 |      template <class T1> class A {
      |                                ^
p415.cpp:39:26: note: previous definition of 'class A<T>'
   39 | template<class T> struct A {
      |                          ^
p415.cpp:160:24: error: qualified name does not name a class before '{' token
  160 |        class A<int>::B {
      |                        ^
p415.cpp:164:13: error: too many template-parameter-lists
  164 |        void A<int>::B<double>::mf1(T t) { }
      |             ^~~~~~
p415.cpp:165:35: error: invalid explicit specialization before '>' token
  165 |      template <class Y> template <>
      |                                   ^
p415.cpp:165:35: error: enclosing class templates are not explicitly specialized
p415.cpp:166:13: error: expected initializer before '<' token
  166 | void A<Y>::B<double>::mf2() { } // error: B<double> is specialized but // its enclosing class template A is not
      |             ^

検討事項(agenda)

コンパイルエラーを取るか、コンパイルエラーの理由を解説する。

参考資料(reference)

cpprefjp - C++日本語リファレンス

コンパイラの実装状況

typedef は C++11 ではオワコン

C99からC++14を駆け抜けるC++講座

自己参照(self reference)

コピペコンパイルエラーあるある

C++ Error Message Collection(1)does not name a type, 11 articles

dockerにclang

docker gnu(gcc/g++) and llvm(clang/clang++)

コンパイル用shell script C版(clangとgcc)とC++版(clang++とg++)

Compare the contents of C++N4910:2022, C++N4741:2018 and C++N4606:2015

C++ sample list

clang++, g++コンパイルエラー方針の違いの例

astyle 使ってみた

C++N4606 Working Draft 2016, ISO/IEC 14882, C++ standardのコード断片をコンパイルするためにしていること
https://qiita.com/kaizen_nagoya/items/a8d7ee2f2e29e76c19c1

コンパイル用shell script C版(clangとgcc)とC++版(clang++とg++)
https://qiita.com/kaizen_nagoya/items/74220c0577a512c2d7da

Clang/Clang++(LLVM) gcc/g++(GNU) コンパイラ警告等比較
https://qiita.com/kaizen_nagoya/items/9a82b958cc3aeef0403f

C++2003とC++2017でコンパイルエラーになるならない事例集
https://qiita.com/kaizen_nagoya/items/a13ea3823441c430edff

Qiitaに投稿するCのStyle例(暫定)
https://qiita.com/kaizen_nagoya/items/946df1528a6a1ef2bc0d

cpprefjpのdecltypeをコンパイル試験
https://qiita.com/kaizen_nagoya/items/090909af702f0d5d8a67

MISRA C++ 5-0-16
https://qiita.com/kaizen_nagoya/items/7df2d4e05db724752a74

C++ Templates Part1 BASICS Chapter 3. Class Templates 3.2 Use of Class Template Stack stack1test.cpp
https://qiita.com/kaizen_nagoya/items/cd5fc49106fad5a4e9ed

ISO/IEC TS 17961:2013 C Secure Coding Rules(1) All list(to be confirmed)
https://qiita.com/kaizen_nagoya/items/54e056195c4f11b850a1

C言語(C++)に対する誤解、曲解、無理解、爽快。
https://qiita.com/kaizen_nagoya/items/3f3992c9722c1cee2e3a

C Puzzle Bookの有り難み5つ、C言語規格及びCコンパイラの特性を認識
https://qiita.com/kaizen_nagoya/items/d89a48c1536a02ecdec9

'wchar.h' file not found で困った clang++ macOS
https://qiita.com/kaizen_nagoya/items/de15cd46d657517fac11

Open POSIX Test Suiteの使い方を調べはじめました
https://qiita.com/kaizen_nagoya/items/644d5e407f5faf96e6dc

MISRA-C 2012 Referenceに掲載している文献の入手可能性を確認
https://qiita.com/kaizen_nagoya/items/96dc8b125e462d5575bb

どうやって MISRA Example Suiteをコンパイルするか
https://qiita.com/kaizen_nagoya/items/fbdbff5ff696e2ca7f00

MISRA C まとめ #include
https://qiita.com/kaizen_nagoya/items/f1a79a7cbd281607c7c9

「C++完全理解ガイド」の同意できること上位10
https://qiita.com/kaizen_nagoya/items/aa5744e0c4a8618c7671

<この記事は個人の過去の経験に基づく個人の感想です。現在所属する組織、業務とは関係がありません。>

文書履歴(document history)

ver. 0.01 初稿  20220706

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?