Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

an example/test uses a dangling reference

オープン
#8 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
説明が足りない
活発さ
停滞
技術スタック
cpp
領域
testing

調査の方向性

libs/local_function/test/return_assign.cpp から始め、既存のテストを実行して、一時オブジェクトの場合とローカル変数の場合を比較します。返された boost::function が束縛された参照をどのように保持するかを追跡し、そのうえで安全なテスト動作として意図されているものを判断します。完了条件は、例が dangling reference に依存しなくなり、関連する Lifetime のケースが正しくカバーされていることです。

索引モデルが issue の本文から書いたものです。

説明

libs/local_function/test/return_assign.cpp uses a dangling reference:

#include <boost/local_function.hpp>
#include <boost/function.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <iostream>

//[return_assign
void call1(boost::function<int (int) > f) { BOOST_TEST(f(1) == 5); }
void call0(boost::function<int (void)> f) { BOOST_TEST(f() == 5); }

boost::function<int (int, int)> linear(const int& slope) {
    int BOOST_LOCAL_FUNCTION(const bind& slope,
            int x, default 1, int y, default 2) {
        return x + slope * y;
    } BOOST_LOCAL_FUNCTION_NAME(lin)

    boost::function<int (int, int)> f = lin; // Assign to local variable.
    BOOST_TEST(f(1, 2) == 5);

    call1(lin); // Pass to other functions.
    call0(lin);

    return lin; // Return.
}

void call(void) {
    boost::function<int (int, int)> f = linear(2); // create temporary, bind reference to temporary
    BOOST_TEST(f(1, 2) == 5); // !!!!!!!!!!!!!!!!!!!!!!!! use the reference
}
//]

int main(void) {
    call();
    return boost::report_errors();
}

changing the target from temporary to local variable can let the test fail:

#include <boost/local_function.hpp>
#include <boost/function.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <iostream>

//[return_assign
void call1(boost::function<int(int) > f) { BOOST_TEST(f(1) == 5); }
void call0(boost::function<int(void)> f) { BOOST_TEST(f() == 5); }

boost::function<int(int, int)> linear(const int& slope) {
    int BOOST_LOCAL_FUNCTION(const bind & slope,
        int x, default 1, int y, default 2) {
        return x + slope * y;
    } BOOST_LOCAL_FUNCTION_NAME(lin)

        boost::function<int(int, int)> f = lin; // Assign to local variable.
    BOOST_TEST(f(1, 2) == 5);

    call1(lin); // Pass to other functions.
    call0(lin);

    return lin; // Return.
}

void call1(void) {
    boost::function<int(int, int)> f = linear(2);
    BOOST_TEST(f(1, 2) == 5); // !!!!!!!!!!!!!!!!!!!!!!!! test passed, although dangling reference
}
void call2(void) {
    int a = 2;
    boost::function<int(int, int)> f = linear(a);
    a = 3;
    BOOST_TEST(f(1, 2) == 5); // !!!!!!!!!!!!!!!!!!!!!!!! test failed
}
//]

int main(void) {
    call1();
    call2();
    return boost::report_errors();
}
主要言語
C++
スター
11
フォーク
28
PR マージ指標
30日以内にマージされた PR はありません

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

boostorg/local_function のほかの issue

boostorg/local_function の issue をすべて見る

似ている issue

C++ の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。