またまた見送られることに
Why Concepts didn’t make C++17
C++2aにおいて三度コンセプトが採択されました。最新の情報はyohhoyさんの下記ブログを参照して下さい。
次期C++2a(C++20)標準仕様に向けて採択された コンセプト(concept) について
というわけで以下は過去の記事です…(´・_・`)
TL;DR
if ( 英語が読める && Conceptを知っている ) {
私が参考にしたバージョンは2015/02/09の物です
(現在も更新乃至修正があります最新版はこちら)
} else {
- これからのC++erの必修科目
- これをスルーするのはスマートポインタを知らない自称C++er並にヤバイ「かもしれない」
}
導入(妄想なので飛ばしてください)
これまでC++は「銃床にナイフの生えたアサルトライフル」と例えられてきたし、 C++で有名なTemplate Meta Programmingの流行に至っては「黒死病」などと言われてきた。貴方も御存知の通り、C++の攻撃力は実際凄まじいものの、防御については割りとどうしようもない面が多い。C++11,14にてメモリ管理を始めとした幾つかの面で防御力の向上が図られたが、黒魔法ことTMPはコンパイル時assertが入ったくらいで、当然デバッガも走らないのだから大変だ。(最近では挙動を試すためのREPLがあるらしいが) 黒魔法はMPではなく頭髪を要求するので、今日も沢山の人々が生え際を消耗しながら黒魔法を唱えているに違いない。所でストロヴストルップ御大の御姿を拝見すると…これ以上はいけない。
ならヘッダオンリーライブラリなどと称する暗黒オーパーツ達は一体どうやって作られたんだ?と気になるかもしれないがTMP司神達の際限を知らぬ暗黒カラテは黒魔法を御する黒魔法を生み出してしまったのである。それは「SFINAE?なにそれ?」という人々にとっても非常に重要であったため、一度は却下されながらもC++1zにて遂にこの人間の住む生成世界へ翻訳される事と相成った。それが今回紹介するコンセプトなのである。
コンセプト
コンセプトはざっくり言うと「期待した式が有効でない場合コンパイルエラーとする機能」だ。実行例を見てもらった方が早いだろう。最近になってC++1zの機能の一部がgcc6.0に実装されたため、我々は以下を試すことができる。なお、今回のコードの試用についてはWandbox様を活用させていただいた。
まず2乗を返す関数を考える。
#include <iostream>
using namespace std;
template<typename T>
auto square(T i) {
return i * i;
}
struct X{};
int main()
{
int a = 3;
struct X b;
cout << square(a) << endl; // OK
cout << square(b) << endl; // Err
return 0;
}
肝心の吐かれるエラーだが、
prog.cc: In instantiation of 'auto square(T) [with T = X]':
prog.cc:16:21: required from here
prog.cc:6:14: error: no match for 'operator*' (operand types are 'X' and 'X')
return i * i;
^
prog.cc: In function 'int main()':
prog.cc:16:10: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'void')
cout << square(b) << endl; // Err
^
In file included from /usr/local/gcc-head/include/c++/6.0.0/iostream:39:0,
from prog.cc:1:
/usr/local/gcc-head/include/c++/6.0.0/ostream:108:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(__ostream_type& (*__pf)(__ostream_type&))
^
/usr/local/gcc-head/include/c++/6.0.0/ostream:108:7: note: no known conversion for argument 1 from 'void' to 'std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&) {aka std::basic_ostream<char>& (*)(std::basic_ostream<char>&)}'
/usr/local/gcc-head/include/c++/6.0.0/ostream:117:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ios_type& (*)(std::basic_ostream<_CharT, _Traits>::__ios_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>; std::basic_ostream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]
operator<<(__ios_type& (*__pf)(__ios_type&))
^
/usr/local/gcc-head/include/c++/6.0.0/ostream:117:7: note: no known conversion for argument 1 from 'void' to 'std::basic_ostream<char>::__ios_type& (*)(std::basic_ostream<char>::__ios_type&) {aka std::basic_ios<char>& (*)(std::basic_ios<char>&)}'
/usr/local/gcc-head/include/c++/6.0.0/ostream:127:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(ios_base& (*__pf) (ios_base&))
^
/usr/local/gcc-head/include/c++/6.0.0/ostream:127:7: note: no known conversion for argument 1 from 'void' to 'std::ios_base& (*)(std::ios_base&)'
/usr/local/gcc-head/include/c++/6.0.0/ostream:166:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(long __n)
^
/usr/local/gcc-head/include/c++/6.0.0/ostream:166:7: note: no known conversion for argument 1 from 'void' to 'long int'
/usr/local/gcc-head/include/c++/6.0.0/ostream:170:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned long __n)
^
/usr/local/gcc-head/include/c++/6.0.0/ostream:170:7: note: no known conversion for argument 1 from 'void' to 'long unsigned int'
/usr/local/gcc-head/include/c++/6.0.0/ostream:174:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(bool __n)
^
/usr/local/gcc-head/include/c++/6.0.0/ostream:174:7: note: no known conversion for argument 1 from 'void' to 'bool'
In file included from /usr/local/gcc-head/include/c++/6.0.0/ostream:638:0,
from /usr/local/gcc-head/include/c++/6.0.0/iostream:39,
from prog.cc:1:
/usr/local/gcc-head/include/c++/6.0.0/bits/ostream.tcc:91:5: note: candidate: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char; _Traits = std::char_traits<char>]
basic_ostream<_CharT, _Traits>::
^
/usr/local/gcc-head/include/c++/6.0.0/bits/ostream.tcc:91:5: note: no known conversion for argument 1 from 'void' to 'short int'
In file included from /usr/local/gcc-head/include/c++/6.0.0/iostream:39:0,
from prog.cc:1:
/usr/local/gcc-head/include/c++/6.0.0/ostream:181:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned short __n)
^
/usr/local/gcc-head/include/c++/6.0.0/ostream:181:7: note: no known conversion for argument 1 from 'void' to 'short unsigned int'
In file included from /usr/local/gcc-head/include/c++/6.0.0/ostream:638:0,
from /usr/local/gcc-head/include/c++/6.0.0/iostream:39,
from prog.cc:1:
/usr/local/gcc-head/include/c++/6.0.0/bits/ostream.tcc:105:5: note: candidate: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char; _Traits = std::char_traits<char>]
basic_ostream<_CharT, _Traits>::
^
/usr/local/gcc-head/include/c++/6.0.0/bits/ostream.tcc:105:5: note: no known conversion for argument 1 from 'void' to 'int'
In file included from /usr/local/gcc-head/include/c++/6.0.0/iostream:39:0,
from prog.cc:1:
/usr/local/gcc-head/include/c++/6.0.0/ostream:192:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned int __n)
^
/usr/local/gcc-head/include/c++/6.0.0/ostream:192:7: note: no known conversion for argument 1 from 'void' to 'unsigned int'
/usr/local/gcc-head/include/c++/6.0.0/ostream:201:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(long long __n)
^
/usr/local/gcc-head/include/c++/6.0.0/ostream:201:7: note: no known conversion for argument 1 from 'void' to 'long long int'
/usr/local/gcc-head/include/c++/6.0.0/ostream:205:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned long long __n)
^
/usr/local/gcc-head/include/c++/6.0.0/ostream:205:7: note: no known conversion for argument 1 from 'void' to 'long long unsigned int'
/usr/local/gcc-head/include/c++/6.0.0/ostream:220:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(double __f)
^
/usr/local/gcc-head/include/c++/6.0.0/ostream:220:7: note: no known conversion for argument 1 from 'void' to 'double'
/usr/local/gcc-head/include/c++/6.0.0/ostream:224:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(float __f)
^
/usr/local/gcc-head/include/c++/6.0.0/ostream:224:7: note: no known conversion for argument 1 from 'void' to 'float'
/usr/local/gcc-head/include/c++/6.0.0/ostream:232:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(long double __f)
^
/usr/local/gcc-head/include/c++/6.0.0/ostream:232:7: note: no known conversion for argument 1 from 'void' to 'long double'
/usr/local/gcc-head/include/c++/6.0.0/ostream:245:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(const void* __p)
^
/usr/local/gcc-head/include/c++/6.0.0/ostream:245:7: note: no known conversion for argument 1 from 'void' to 'const void*'
In file included from /usr/local/gcc-head/include/c++/6.0.0/ostream:638:0,
from /usr/local/gcc-head/include/c++/6.0.0/iostream:39,
from prog.cc:1:
/usr/local/gcc-head/include/c++/6.0.0/bits/ostream.tcc:119:5: note: candidate: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]
basic_ostream<_CharT, _Traits>::
^
/usr/local/gcc-head/include/c++/6.0.0/bits/ostream.tcc:119:5: note: no known conversion for argument 1 from 'void' to 'std::basic_ostream<char>::__streambuf_type* {aka std::basic_streambuf<char>*}'
In file included from /usr/local/gcc-head/include/c++/6.0.0/string:52:0,
from /usr/local/gcc-head/include/c++/6.0.0/bits/locale_classes.h:40,
from /usr/local/gcc-head/include/c++/6.0.0/bits/ios_base.h:41,
from /usr/local/gcc-head/include/c++/6.0.0/ios:42,
from /usr/local/gcc-head/include/c++/6.0.0/ostream:38,
from /usr/local/gcc-head/include/c++/6.0.0/iostream:39,
from prog.cc:1:
/usr/local/gcc-head/include/c++/6.0.0/bits/basic_string.h:5201:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/local/gcc-head/include/c++/6.0.0/bits/basic_string.h:5201:5: note: template argument deduction/substitution failed:
prog.cc:16:21: note: mismatched types 'const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>' and 'void'
cout << square(b) << endl; // Err
^
In file included from /usr/local/gcc-head/include/c++/6.0.0/bits/ios_base.h:46:0,
from /usr/local/gcc-head/include/c++/6.0.0/ios:42,
from /usr/local/gcc-head/include/c++/6.0.0/ostream:38,
from /usr/local/gcc-head/include/c++/6.0.0/iostream:39,
from prog.cc:1:
/usr/local/gcc-head/include/c++/6.0.0/system_error:209:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::error_code&)
operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
^
/usr/local/gcc-head/include/c++/6.0.0/system_error:209:5: note: template argument deduction/substitution failed:
prog.cc:16:19: note: cannot convert 'square<X>((b, X()))' (type 'void') to type 'const std::error_code&'
cout << square(b) << endl; // Err
^
In file included from /usr/local/gcc-head/include/c++/6.0.0/iostream:39:0,
from prog.cc:1:
/usr/local/gcc-head/include/c++/6.0.0/ostream:497:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, _CharT)
operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
^
/usr/local/gcc-head/include/c++/6.0.0/ostream:497:5: note: template argument deduction/substitution failed:
prog.cc:16:21: note: deduced conflicting types for parameter '_CharT' ('char' and 'void')
cout << square(b) << endl; // Err
^
In file included from /usr/local/gcc-head/include/c++/6.0.0/iostream:39:0,
from prog.cc:1:
/usr/local/gcc-head/include/c++/6.0.0/ostream:502:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char)
operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
^
/usr/local/gcc-head/include/c++/6.0.0/ostream:502:5: note: template argument deduction/substitution failed:
prog.cc:16:19: note: cannot convert 'square<X>((b, X()))' (type 'void') to type 'char'
cout << square(b) << endl; // Err
^
In file included from /usr/local/gcc-head/include/c++/6.0.0/iostream:39:0,
from prog.cc:1:
/usr/local/gcc-head/include/c++/6.0.0/ostream:508:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char)
operator<<(basic_ostream<char, _Traits>& __out, char __c)
^
/usr/local/gcc-head/include/c++/6.0.0/ostream:508:5: note: template argument deduction/substitution failed:
prog.cc:16:19: note: cannot convert 'square<X>((b, X()))' (type 'void') to type 'char'
cout << square(b) << endl; // Err
^
In file included from /usr/local/gcc-head/include/c++/6.0.0/iostream:39:0,
from prog.cc:1:
/usr/local/gcc-head/include/c++/6.0.0/ostream:514:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char)
operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
^
/usr/local/gcc-head/include/c++/6.0.0/ostream:514:5: note: template argument deduction/substitution failed:
prog.cc:16:19: note: cannot convert 'square<X>((b, X()))' (type 'void') to type 'signed char'
cout << square(b) << endl; // Err
^
In file included from /usr/local/gcc-head/include/c++/6.0.0/iostream:39:0,
from prog.cc:1:
/usr/local/gcc-head/include/c++/6.0.0/ostream:519:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char)
operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
^
/usr/local/gcc-head/include/c++/6.0.0/ostream:519:5: note: template argument deduction/substitution failed:
prog.cc:16:19: note: cannot convert 'square<X>((b, X()))' (type 'void') to type 'unsigned char'
cout << square(b) << endl; // Err
^
In file included from /usr/local/gcc-head/include/c++/6.0.0/iostream:39:0,
from prog.cc:1:
/usr/local/gcc-head/include/c++/6.0.0/ostream:539:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const _CharT*)
operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
^
/usr/local/gcc-head/include/c++/6.0.0/ostream:539:5: note: template argument deduction/substitution failed:
prog.cc:16:21: note: mismatched types 'const _CharT*' and 'void'
cout << square(b) << endl; // Err
^
In file included from /usr/local/gcc-head/include/c++/6.0.0/ostream:638:0,
from /usr/local/gcc-head/include/c++/6.0.0/iostream:39,
from prog.cc:1:
/usr/local/gcc-head/include/c++/6.0.0/bits/ostream.tcc:321:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*)
operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
^
/usr/local/gcc-head/include/c++/6.0.0/bits/ostream.tcc:321:5: note: template argument deduction/substitution failed:
prog.cc:16:19: note: cannot convert 'square<X>((b, X()))' (type 'void') to type 'const char*'
cout << square(b) << endl; // Err
^
In file included from /usr/local/gcc-head/include/c++/6.0.0/iostream:39:0,
from prog.cc:1:
/usr/local/gcc-head/include/c++/6.0.0/ostream:556:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*)
operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
^
/usr/local/gcc-head/include/c++/6.0.0/ostream:556:5: note: template argument deduction/substitution failed:
prog.cc:16:19: note: cannot convert 'square<X>((b, X()))' (type 'void') to type 'const char*'
cout << square(b) << endl; // Err
^
In file included from /usr/local/gcc-head/include/c++/6.0.0/iostream:39:0,
from prog.cc:1:
/usr/local/gcc-head/include/c++/6.0.0/ostream:569:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*)
operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
^
/usr/local/gcc-head/include/c++/6.0.0/ostream:569:5: note: template argument deduction/substitution failed:
prog.cc:16:19: note: cannot convert 'square<X>((b, X()))' (type 'void') to type 'const signed char*'
cout << square(b) << endl; // Err
^
In file included from /usr/local/gcc-head/include/c++/6.0.0/iostream:39:0,
from prog.cc:1:
/usr/local/gcc-head/include/c++/6.0.0/ostream:574:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*)
operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
^
/usr/local/gcc-head/include/c++/6.0.0/ostream:574:5: note: template argument deduction/substitution failed:
prog.cc:16:19: note: cannot convert 'square<X>((b, X()))' (type 'void') to type 'const unsigned char*'
cout << square(b) << endl; // Err
^
In file included from /usr/local/gcc-head/include/c++/6.0.0/iostream:39:0,
from prog.cc:1:
/usr/local/gcc-head/include/c++/6.0.0/ostream:628:5: note: candidate: template<class _CharT, class _Traits, class _Tp> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&)
operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
^
/usr/local/gcc-head/include/c++/6.0.0/ostream:628:5: note: template argument deduction/substitution failed:
/usr/local/gcc-head/include/c++/6.0.0/ostream: In substitution of 'template<class _CharT, class _Traits, class _Tp> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = void]':
prog.cc:16:21: required from here
/usr/local/gcc-head/include/c++/6.0.0/ostream:628:5: error: forming reference to void
とまぁ、お馴染みの画面が出る。これらnoteも手掛かりには成るのだが、複数のエラーが組み合わさっている場合、この塊を目grepする必要がある。つらい。ではコンセプトがあるとどうなるか。
#include <iostream>
using namespace std;
template<typename T>
concept bool Multipliable =
requires (T a, T b) {
a * b;
};
auto square(Multipliable i) {
return i * i;
}
struct X{};
int main()
{
int a = 3;
struct X b;
cout << square(a) << endl; // OK
cout << square(b) << endl; // Err
return 0;
}
エラーは、
prog.cc: In function 'int main()':
prog.cc:21:21: error: cannot call function 'auto square(auto:1) [with auto:1 = X]'
cout << square(b) << endl; // NG
^
prog.cc:10:6: note: constraints not satisfied
auto square(Multipliable i) {
^
prog.cc:10:6: note: concept 'Multipliable<X>' was not satisfied
あるコンセプトが満たされていないこと、その宣言場所、そして原因となった関数呼び出し、という実に簡潔なメッセージである。素晴らしい。
簡単な解説
一旦ここで簡単な解説を行う。TMPを触ったことの有る御仁ならば先ほどのコードだけで殆ど察しただろうし次に進んでもらって構わない。
#include <iostream>
using namespace std;
template<typename T>
concept bool Multipliable = // コンセプト
requires (T a, T b) {
a * b;
}; // requires式
auto square(Multipliable i) { // コンセプトを用いた関数宣言
return i * i;
}
struct X{};
int main()
{
int a = 3;
struct X b;
cout << square(a) << endl; // OK
cout << square(b) << endl; // NG
return 0;
}
さて、推論がより強力な言語であればまだ良いが、エラーメッセージのとんでもなさから「コンパイルエラーの爆発量を競う大会」が開かれるほどのC++において、「あらゆる型を通す」などと言うのはより効率良く禿げてしまうだけだ。我々に必要なのは「最低の条件を満たすあらゆる型」なのである。
コンセプトはその「条件を満たしたあらゆる型」を表現する軽量な構文である。まずいつものtemplate<typename T>
で「あらゆる型」を宣言した後、続けてこのT
が満たして欲しい式を列挙していく。
requires式の部分では、先に宣言したT
型の変数について考える。このケースでは、T
型の2つの変数についてa * b
を行えるかが争点となる。requires式では常にエラーとなるもの以外の式について記述でき、
template<typename T> concept bool C = requires (T t) { t.f(); };
のようなメンバ関数呼び出しの確認も当然可能である。なお、これら変数は具体的な値ではないため、a == 0
といった式はそもそも書けないことに注意してほしい。コンセプトを定義したら、その名前を関数の引数の型として用いることで「コンセプトを満たすあらゆる型」を表現できる。また、
template <Multipliable T>
auto square(T i) { return i * i; }
のようにテンプレート引数にコンセプトを適用することも可能だ。
コンセプトの機能
今のままでもそれなりに便利であるが、コンセプトにはもう少し色々な機能があるようなのでもう少し調べてみる。
コンセプト単体
まず、コンセプトとrequires式は別物だ、というかコンセプトは単体で使うことができる。
コンセプトは右辺の式の真偽によってコンパイルを通すか否か決定する。
#include <iostream>
using namespace std;
template<typename T> concept bool SmallerDouble = sizeof(T) < sizeof(double);
int main()
{
auto psize = [](SmallerDouble a){ cout << sizeof(a) << endl; }; // ジェネリックラムダ!
int vint = 1;
double vdouble = 1.0;
psize(vint); // OK
psize(vdouble); // Err
return 0;
}
ジェネリックラムダにも対応している。推論の分か、通常よりもエラーメッセージが多いけれども許容範囲内だろう。
型要求
先程は式が有効である制約(式制約)を追加する要求だったが、型の定義に関しても制約が追加可能だ。
template<typename T> struct S { };
template<typename T> using Ref = T&;
template<typename T> concept bool C =
requires () {
typename T::inner; // T内にinnerが定義されているか
typename S<T>; // SをTで「テンプレートのインスタンス化」ができるか
typename Ref<T>; // usingエイリアスも勿論使用可能
};
struct X{ using inner = int; }; // 例えばこれが受理される
この例では記述していないが、type_traitsヘッダのメタ関数群を使用して型の特性を確認することもできるだろう。
複合要求
式制約に制約を追加する要求。(この辺は物によって上手く動かなかったので、仕様のサンプルの受け売り)
template<typename T> concept bool C2 =
requires(T x) {
{*x} -> typename T::inner;
};
例えばこれは
-
*x
の式制約 -
typename T::inner
の型制約 -
*x
の結果の型がT::inner
の型へ暗黙的変換できるという変換制約
という3つの制約を追加する要求になる。
template<typename T> concept bool C3 =
requires(T x) {
{g(x)} noexcept;
};
これはその通り
-
g(x)
の式制約 -
g(x)
について例外が発生しないという例外制約
という要求になる。
型変換だけではなく、型推論による制約も可能である。(今回試した限りでは上手く動かせなかったが…)
template<typename T, typename U>
struct Pair;
template<typename T>
concept bool C1() { return true; }
template<typename T>
concept bool C2() { return requires(T t) { {*t} -> Pair<C1&, auto>; }; }
-
*t
の式制約 -
*t
がPair<C1&, auto>
と推論できるかという推論制約-
Pair
のテンプレート引数の左は更にC1
で制約 -
auto
とすると、制約なしで推論できる
-
コンセプトは=以外に関数のような形式で記述することも可能なようだ。
コンセプトの合成
コンセプトは&&
や||
を用いて合成可能だ。
template<typename T> concept bool C = requires (T t) { t.f(); };
template<typename T> concept bool D = C<T> && requires (T t) { t.g(); };
requires式の中にrequires式をネストすることで合成することもできる。
template<typename T> concept bool C() { return sizeof(T) == 1; }
template<typename T> concept bool D =
requires (T t) {
requires C<decltype (+t)>();
};
また、関数側で制約を記述できるrequires節なるものが追加される。
template<typename T> requires C<T> && D<T> void f1(T);
auto f2(int a) -> bool requires true; // 返り値後置の記法ではこう
// なお、仮想メンバ関数には付けられない
まとめ
ようやくこれでTMPによるテクニックを学ばずともまともなジェネリックプログラミングができるようになる筈だ。とはいえ標準でサポートされるTMPの領域がどんどん増えつつ有るし、TMPがわからないとc++1zの更新内容の幾つかは意味もわからないのが現状なのだが…
(RustやDに移行すると幸せになれることが知られているぞい(◔⊖◔))
え?こんな機能をどうやってTMPで実現していたんだだって?ようこそ目眩くTMPの世界へ!今ならC++テンプレートテクニック第2版がおすすめだ!
なおnested-name-specifier
とtemplate-introduction
は一旦スルーとさせて頂きます。
間違っているor補足すべき部分orいやこここうすれば動くでしょ があればご指摘宜しくお願いします。