Menu

#69 Return owl::tstring or owl::tstring_view for functions currently returning LPCTSTR

8
open
nobody
API (116)
1
2025-03-31
2014-05-04
No

Returning LPCTSTR, i.e. a pointer, may lead to pointer comparisons where string comparisons were intended. For example:

:::C++
void Foo(TTreeNode& n)
{
  n.SetText("Oops!");
  if (n.GetText() == "Oops!") // Pointer comparison, never true.
    CHECK(false);
}

With the ubiquitous use of std::string in modern C++ programming, the likelihood of this pitfall has increased.

Related

Feature Requests: #127

Discussion

  • Vidar Hasfjord

    Vidar Hasfjord - 2023-09-10

    Where returning owl::tstring is not feasible or will be costly, e.g. for returning lookup results in a dictionary of static strings (such as the Windows message names in "eventhan.cpp"), we can now use owl::tstring_view [feature-requests:#127], which is a lightweight pointer and size with a string-like API. It is an alias for std::string_view or std::wstring_view, introduced in C++17.

    This will eliminate bugs such as win.GetCaption() == "MyCaption", which is a pointer comparison, rather than the string comparison that was intended.

    What do you think? Something to implement in OWLNext 8?

     

    Related

    Feature Requests: #127


    Last edit: Vidar Hasfjord 2025-03-31
    • Ognyan Chernokozhev

      What do you think? Something to implement in OWLNext 8?

      Yes, I think reducing of raw pointers usage makes sense for safety.

       

Anonymous
Anonymous

Add attachments
Cancel





MongoDB Logo MongoDB