先日、C++ポケットリファレンスを読んでいたところ、宇宙船演算子なるものを見つけました。
これは演算子一覧表に載っておらず、宇宙船演算子の項にも「ユーザー定義型の演算子」とあり、使うのにわざわざ<compare>をインクルードしなければならないということから、演算子は1から自作できるのだと思いました。早速以下のようなソースをコンパイルしましたが、8行目でエラーが出ました(4行目でエラーは出ていない)。
C++
1#include <stdio.h> 2#include <math.h> 3// 左オペランドの右オペランド乗を返したい 4int operator$(int x , int n) { return pow(x , n); } 5 6int main() 7{ 8 printf("2の3乗は%dです" , 2 $ 3); 9 10 return 0; 11} 12
どうすれば解決できるでしょうか。
環境
OS : Windows8.1 x64
コンパイラ : MinGW-W64
> gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=d:/apps/mingw-w64/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/11.2.0/lto-wrapper.exe OFFLOAD_TARGET_NAMES=nvptx-none Target: x86_64-w64-mingw32 Configured with: ../configure --prefix=/R/winlibs64ucrt_stage/inst_gcc-11.2.0/share/gcc --build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --enable-offload-targets=nvptx-none --with-pkgver sion='MinGW-W64 x86_64-ucrt-posix-seh, built by Brecht Sanders' --with-tune=generic --enable-checking=release --enable-threads=posix --disable-sjlj-exceptions --disable-libunwind-exceptions --disable-serial-configure --disable-bootstrap --enable-host-shared --enable-plugin --disable-default-ssp --disable-rpath --enable-libstdcxx-pch --enable-libstdcxx-time=yes --disable-libstdc xx-debug --disable-version-specific-runtime-libs --with-stabs --disable-symvers --enable-languages=c,c++,fortran,lto,objc,obj-c++,jit --disable-gold --disable-nls --disable-stage1-checking - -disable-win32-registry --disable-multilib --enable-ld --enable-libquadmath --enable-libada --enable-libssp --enable-libstdcxx --enable-lto --enable-fully-dynamic-string --enable-libgomp --e nable-graphite --enable-mingw-wildcard --with-mpc=/d/Prog/winlibs64ucrt_stage/custombuilt --with-mpfr=/d/Prog/winlibs64ucrt_stage/custombuilt --with-gmp=/d/Prog/winlibs64ucrt_stage/custombui lt --with-isl=/d/Prog/winlibs64ucrt_stage/custombuilt --enable-install-libiberty --enable-__cxa_atexit --without-included-gettext --with-diagnostics-color=auto --enable-clocale=generic --wit h-libiconv --with-system-zlib --with-build-sysroot=/R/winlibs64ucrt_stage/gcc-11.2.0/build_mingw/mingw-w64 CFLAGS=-I/d/Prog/winlibs64ucrt_stage/custombuilt/include/libdl-win32 Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 11.2.0 (MinGW-W64 x86_64-ucrt-posix-seh, built by Brecht Sanders)
ユーザー定義型の演算子というのは、ユーザーが定義した型の演算子という意味です。
https://onihusube.hatenablog.com/entry/2019/01/13/180100
C++ が用意している演算子をオーバーロードはできるけど、C++ が用意していない適当な記号を演算子として使うことはできないのでは?
で、<=> は C++20 から追加された「C++ が用意している演算子」なので、演算子ではない $ を演算子として定義することはできない、ということでは?
<=>は「C++が用意している演算子」ではないと思います。
もし組み込みならばわざわざ<compare>をインクルードさせるわけがありません。
C++ が用意している演算子です。
読まない人に何を書いても仕方がないけど。
https://cpprefjp.github.io/lang/cpp20/consistent_comparison.html
> もし組み込みならばわざわざ<compare>をインクルードさせるわけがありません。
それがマチガイ。
<=> 演算子が評価された結果の型は std::strong_ordering です。
したがってstd::strong_orderingのふるまいが定義された<compare>を#includeしないと
<=> が(実質)使えません。
今のバージョンではできないんですね。
将来的にできるようになることを願っておきます。
まあ、ずっと無理でしょうね。
用意していない適当な記号を演算子として使うとなると、コンパイラの字句解析がとんでもないことになりそうなので...かなり悲観的にならざるを得ない。
回答1件