CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Sudden problem with std::vector

    Code:
    uint32_t some_func() { return 32; }
    
    int buf_1[32];          // <--- this always compiled with MSVC
    int buf_2[some_func()]; // <--- whereas this fails because the buffer size needs to be known at compile time
    In the past I always got around this by using std::vector - i.e.

    Code:
    uint32_t some_func() { return 32; }
    
    const int x = some_func(); // <--- I'm sure these lines
    std::vector<int> buf_3[x]; // <--- previously compiled
    But all of a sudden this morning, the std::vector line is giving me C2131 because the value of x isn't known at compile time. I'm probably missing something obvious but I'm sure this used to compile okay... can anyone see what I'm doing wrong?
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  2. #2
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Sudden problem with std::vector

    Oops - I was using square brackets instead of round

    This works...

    Code:
    std::vector<int> buf_3(x);
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  3. #3
    Join Date
    Feb 2017
    Posts
    677

    Re: Sudden problem with std::vector

    Quote Originally Posted by John E View Post
    Code:
    int buf_2[some_func()]; // <--- whereas this fails because the buffer size needs to be known at compile time
    It will work (in this case) if the function is declared constexpr.

  4. #4
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Sudden problem with std::vector

    Thanks for the tip wolle. I'd never even heard of constexpr but you're right - it works fine!
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  5. #5
    Join Date
    Feb 2017
    Posts
    677

    Re: Sudden problem with std::vector

    Quote Originally Posted by John E View Post
    it works fine!
    But, it only works if the function itself is executable at compile-time. If not, you still need to use an std::vector.

  6. #6
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Sudden problem with std::vector

    Ah yes, you're right - in fact even something as simple as this doesn't work...

    Code:
    constexpr uint32_t some_func() { return sqrt(1024); }
    Last edited by John E; December 4th, 2021 at 09:16 AM.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Sudden problem with std::vector

    For a constexpr function to return a constexpr value, any functions it calls must also be marked as constexpr. ie. can be evaluated at compile time. Unfortunately, sqrt() isn't one of them.

    See https://en.cppreference.com/w/cpp/language/constexpr

    and the example of a compile-time factorial function.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured