コンテンツにスキップ

コメントアウトのトリック

プログラムで使えるコメントアウトのトリックを紹介します。
開発中のちょっとしたデバッグや、ミスの防止に活用できます。

範囲の ON・OFF

スラッシュ / の有無に応じて、範囲コメントの有効・無効を切り替えます。

パターン 1

#include <iostream>

int main()
{
    /**/
    int x;

    std::cin >> x;

    std::cout << x * x;
    /**/
}
#include <iostream>

int main()
{
    /**
    int x;

    std::cin >> x;

    std::cout << x * x;
    /**/
}

パターン 2

#include <iostream>

int main()
{
    //*
    int x;

    std::cin >> x;

    std::cout << x * x;
    //*/
}
#include <iostream>

int main()
{
    /*
    int x;

    std::cin >> x;

    std::cout << x * x;
    //*/
}

値の切り替え

スラッシュ / の有無に応じて、左右どちらかの値を選択します。

#include <iostream>

int main()
{
    constexpr int N = /**/ 100 /*/ 200 /**/;

    std::cout << N << '\n';
}
#include <iostream>

int main()
{
    constexpr int N = /** 100 /*/ 200 /**/;

    std::cout << N << '\n';
}

範囲の切り替え

値の切り替えの範囲版です。
スラッシュ / の有無に応じて、前半、後半どちらかの範囲を選択します。

パターン 1

#include <iostream>

int main()
{
    /**/
    int x;

    std::cin >> x;

    std::cout << x * x;
    /*/
    int x, y;

    std::cin >> x >> y;

    std::cout << x + y;
    /**/
}
#include <iostream>

int main()
{
    /**
    int x;

    std::cin >> x;

    std::cout << x * x;
    /*/
    int x, y;

    std::cin >> x >> y;

    std::cout << x + y;
    /**/
}

パターン 2

#include <iostream>

int main()
{
    //*
    int x;

    std::cin >> x;

    std::cout << x * x;
    /*/
    int x, y;

    std::cin >> x >> y;

    std::cout << x + y;
    //*/
}
#include <iostream>

int main()
{
    /*
    int x;

    std::cin >> x;

    std::cout << x * x;
    /*/
    int x, y;

    std::cin >> x >> y;

    std::cout << x + y;
    //*/
}

行の入れ替えの防止

リファクタリング時に、コピー&ペーストで行の順番を入れ替えてしまうことを防ぎます。

void First() {}
void Second() {}

int main()
{
    First();/*
    */Second();
}
void First() {}
void Second() {}

int main()
{
    */ Second();
    First();/*  
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy