switch statement case label has only `int` template argument

Open
#19 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
cpp
Domain
backend

Research direction

Start by locating the switch_statement and case_statement entry points named in the report, then reproduce the sample with the stated compiler behavior. Trace how the case value and lambda argument are typed; done means an integral case value such as 2147483648 is represented and matched correctly instead of producing “other,” with regression coverage for the behavior.

Written by the indexing model from the issue text.

Description

Before gcc9, if a template argument is an integral value, no compiler error/warning generated when the specified value is bigger than the integral value MAX.

This leads the following runtime issue:

#include <iostream>

#include <boost/lambda/lambda.hpp>
#include <boost/lambda/switch.hpp>

int main() {
    using namespace boost;
    using namespace boost::lambda;
    
    switch_statement(
        _1,
        case_statement<2147483648>(std::cout << constant("big")),
        default_statement(std::cout << constant("other"))
    )(2147483648); // prints 'other'
    // static_cast<int>(2147483648) is OK
}

This issue prone working can be eliminated in any following way:

  • replace case_statement template argument from int to uintmax_t or intmax_t or any bigger than int. This solves that not only int parameters can be accepted in later gcc's and clang too.
  • switch_statement lambda accepts/casts argument int. This solves type safety.
  • some other template magic, where at case statement can be set the acceptable integral type, and switch statement recognizes, and uses common_type to casts lambda parameter to that.
Dominant language
C++
Stars
20
Forks
42
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from boostorg/lambda

All issues in boostorg/lambda

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.