Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix a bug in isAutoCreationClass<T>. #2277

Merged
merged 4 commits into from
Mar 22, 2025

Conversation

tanglong3bf
Copy link
Contributor

DrObject::DrAllocator::registerClass() 中的 else if 分支,isAutoCreationClass<D>::value 总会是 false ,因为其内部的偏特化始终没有被使用。
测试代码:

#include <drogon/DrObject.h>
using namespace drogon;

class A
{
};

class B
{
  public:
    static constexpr bool isAutoCreation = true;
};

class C
{
  public:
    static constexpr bool isAutoCreation = false;
};

class D
{
  public:
    static constexpr double isAutoCreation = 3.0;
};

int main()
{
    if constexpr (isAutoCreationClass<A>::value)
    {
        std::cout << "A" << std::endl;
    }
    // only this case will be true
    if constexpr (isAutoCreationClass<B>::value)
    {
        std::cout << "B" << std::endl;
    }
    if constexpr (isAutoCreationClass<C>::value)
    {
        std::cout << "C" << std::endl;
    }
    if constexpr (isAutoCreationClass<D>::value)
    {
        std::cout << "D" << std::endl;
    }
    return 0;
}

@marty1885
Copy link
Member

謝謝發現這個問題。但感覺應該疏漏了 std::decay_t?強迫測試 const bool 好像反而會有其他邊緣狀況不會捕捉到。

@an-tao
Copy link
Member

an-tao commented Mar 19, 2025

謝謝發現這個問題。但感覺應該疏漏了 std::decay_t?強迫測試 const bool 好像反而會有其他邊緣狀況不會捕捉到。

@tanglong3bf 谢谢,建议补上单元测试,各种情况用例尽量写全。

Comment on lines 63 to 69
DROGON_TEST(IsAutoCreationClassTest)
{
CHECK(isAutoCreationClass<TestA>::value == false);
CHECK(isAutoCreationClass<TestC>::value == true);
CHECK(isAutoCreationClass<TestD>::value == false);
CHECK(isAutoCreationClass<TestE>::value == false);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use STATIC_REQUIRE so it checks at compile time.

@an-tao an-tao merged commit 1fb67d6 into drogonframework:master Mar 22, 2025
32 of 35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants