GeezerVison is showing github.com — C++ の面白いコード、トリッキーなコードの話 · Issue #9 · cppmap/cppmap.docs
C++ の面白いコード、トリッキーなコードの話 #9Description実用性はなくてもいいので、面白いネタ的なコードや不思議な文法、仕様のハックなどがあれば教えてください。ある程度たまったら記事にします。
- 数行~十数行で完結するぐらいが好ましいです
- マイナーなものでも有名なものでも OK です
(例) コメントアウトのトリック Activityfaithandbrave commented on Mar 12, 2019 faithandbrave commented on Mar 12, 2019 プリプロセス関係だと、文字列リテラルの結合とかも、マイナーですがたまに便利ですね。自前のprintfやassertを作るときにも使いますし、長い文字列リテラルを分割して書くのにも便利です。
#include <cstdio>
#define INFO(fmt, ...) std::printf("[info] " fmt "\n" __VA_OPT__(,) __VA_ARGS__)
#define DEBUG(fmt, ...) std::printf("[debug] " fmt "\n" __VA_OPT__(,) __VA_ARGS__)
int main()
{
INFO("hello");
DEBUG("hello %d", 123);
}
#include <iostream>
int main()
{
std::string help = "The application is designed by ME.\n"
"Welcome my application";
std::cout << help << std::endl;
}Reacted by Ryo Suzuki and onihusube faithandbrave commented on Mar 12, 2019 csvのインクルードとかを多用するようになると、以前私がブログに書いた以下のようなものがほしくなります。
Reputeless commented on Mar 12, 2019 コメントありがとうございます! 引き続き募集してます。
--> 演算子
#include <iostream>
int main()
{
int i = 10;
while (i --> 0)
{
std::cout << i << '\n';
}
}wx257osn2 commented on Mar 12, 2019 template関数のオーバーロードに優先順位を付けるやつ:
#include<cstdint>
template<std::size_t N>struct priority : priority<N-1>{};
template<> struct priority<0>{};
#include<iostream>
#include<type_traits>
template<
typename T,
std::enable_if_t<std::is_same<std::decay_t<T>, float>::value, std::nullptr_t> = nullptr
>
void f_impl(T t, priority<1>){std::cout << "float " << t << std::endl;}
template<typename T>
void f_impl(T t, priority<0>){std::cout << "T " << t << std::endl;}
template<typename T>
void f(T&& t){f_impl(std::forward<T>(t), priority<1>{});}
int main(){
f(3.1);
f(3.14f);
f("3.141592");
}
Wandbox onihusube commented on Mar 12, 2019 面白いかは分からないですが、リテラル0だけを受け取る関数(nullptrからは目を逸らして・・・)
struct minus {
constexpr bool operator<(std::nullptr_t) {
return true;
}
constexpr bool operator>(std::nullptr_t) {
return false;
}
};
void f(std::nullptr_t) {
std::cout << "literal 0" << std::endl;
}
https://wandbox.org/permlink/sDe2GzeQuPk1hMT0 Reacted by Ryo Suzuki and yumetodo Fuyutsubaki commented on Mar 12, 2019 Reacted by Ryo Suzuki Reacted by yoh yumetodo commented on Mar 13, 2019 yumetodo commented on Mar 13, 2019 input streamの中身をまるごとoutput streamにぶちこむやつ
rhysd commented on Mar 14, 2019 agehama commented on Mar 14, 2019 暗黙の型変換にやられるやつ(ただのバグ)
#include <iostream>
int main()
{
if(-1 < 1u)
{
std::cout << "-1 < 1u";
}
else
{
std::cout << "-1 > 1u";
}
}
agehama commented on Mar 14, 2019 名前空間の付け忘れにやられるやつ(ただのバグ)
#include <iostream>
#include <cmath>
int main()
{
//std::cout << std::abs(0.5); // 0.5
std::cout << abs(0.5); // 0
}
nekko1119 commented on Mar 16, 2019 template <int>
int a = 0;
int main() {
//a<10>= 1; // 動かない
a<10> = 1;
}
スペースの有無で演算子と解釈されるかどうか異なります Reacted by Ryo Suzuki and yumetodo Fuyutsubaki commented on Mar 18, 2019 (特別C++に限らないけけど) Fast inverse square root とか zxc-key commented on Oct 27, 2020 yumetodo commented on Oct 27, 2020 constexprが普通に使えるこんにちは出番がなさそう・・・? Reputeless commented on Oct 27, 2020 @zxc-key 提案ありがとうございます。
enum をコンパイル時定数として扱うのは今では古いテクニック (現在は constexpr) で、ある程度知られているとも思います
- 「最後尾 = 要素数」は、サンプルコードでいうと
_0 = 100 のようなコードで不正になります。magic_enum というライブラリの enum_count では、複雑なテクニックでその問題も回避しています
- cppmap 掲載済みです https://cppmap.github.io/articles/it-compiles/#_2
【面白いネタ的なコードや不思議な文法】として cppmap で新規に取り上げるネタはありませんでしたが、また面白いコードを見つけたら教えてください 🖐 yohhoy commented on Mar 5, 2021 yaito3014 commented on Mar 7, 2021 #include <iostream>
using T = int&;
void f(T&) { std::cout << "non-const" << std::endl; }
void f(const T&) { std::cout << "const" << std::endl; }
int main() {
int x;
f(x);
}
コンパイルできそうでできないやつです。 yumetodo commented on Mar 7, 2021 Ryoga-exe commented on Mar 8, 2021 長い演算子 (演算子の優先順位的にできるやつ)
#include <iostream>
int main() {
int a = 10, b = 10;
a = a +-+-+-+-+-+-+-+-+ 6 +-+-+-+-+-+-+-+-+ 5;
b = b + + + + + + + + + 6 - - - - - - - - - 5;
std::cout << a << '\n';
std::cout << b << '\n';
return 0;
}faithandbrave commented on Mar 11, 2025 assert(!"message");は有名ですね。
#include <cassert>
#include <iostream>
void f(int x) {
if (x >= 0) {
std::cout << x << std::endl;
}
else {
assert(!"f() argument be positive");
}
}
int main() {
f(-1);
}
出力:
prog.exe: prog.cc:9: void f(int): Assertion `!"f() argument be positive"' failed.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|