error: in-class initializer for static data member is not a constant expression [clang-diagnostic-error]

Open
#1,107 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
cpp
Domain
compilers

Research direction

Reproduce the diagnostic from ../src/configuration.cpp, then inspect Boost's is_signed.hpp and is_unsigned.hpp along the instantiation path through program_options/value_semantic.hpp. Confirm the behavior for boost::log::trivial::severity_level and its default_value call; done means the reported Clang errors no longer occur for this case.

Written by the indexing model from the issue text.

Description

Code:

// NOLINTBEGIN(cppcoreguidelines-pro-type-member-init)
MPingSender::Configuration::Configuration(const std::span<char *> args)
{
    boost::program_options::options_description options{"Configuration"};
    // clang-format off
    options.add_options()
      ("help", "Help screen")
      ("log-level", boost::program_options::value<boost::log::trivial::severity_level>(&this->_log_level)->default_value(boost::log::trivial::info), "Log level. Valid options are trace, debug, info, warning, error and fatal. Defaults to info.")
      ("bind-address", boost::program_options::value<std::string>(&this->_bind_address)->required(), "Address to which the UDP socket is bound. Required.")
      ("bind-port", boost::program_options::value<uint16_t>(&this->_bind_port)->default_value(4321), "Port to which the UDP socket is bound. Defaults to 4321.")
      ("address", boost::program_options::value<std::string>(&this->_address)->default_value("ff2e::42"), "Address to send packets to. Defaults to ff2e::42")
      ("port", boost::program_options::value<uint16_t>(&this->_port)->default_value(4321), "Address to which the packages are sent.")
      ("ttl", boost::program_options::value<uint8_t>(&this->_ttl)->default_value(64), "TTTL or hop limit with which the packets are sent. Defaults to 64.")
      ("interface-name", boost::program_options::value<std::string>(&this->_interface_name)->required(), "Name of the interface from which the packets are sent. Required.")
    ;
    // clang-format on

    boost::program_options::command_line_parser parser{
        static_cast<int>(args.size()), args.data()};
    parser.options(options).allow_unregistered().style(
        boost::program_options::command_line_style::allow_long |
        boost::program_options::command_line_style::allow_sticky |
        boost::program_options::command_line_style::long_allow_next);
    const boost::program_options::parsed_options parsed_options = parser.run();

    boost::program_options::variables_map vm;
    store(parsed_options, vm);

    this->_help = vm.contains("help");
    if (this->_help)
    {
        std::clog << "mping-sender" << std::endl
                  << std::endl
                  << "Usage: mping-sender [configuration]" << std::endl
                  << std::endl
                  << options << std::endl;
        std::exit(EXIT_SUCCESS); // NOLINT(concurrency-mt-unsafe)
    }

    notify(vm);

    boost::log::core::get()->set_filter(boost::log::trivial::severity >=
                                        this->_log_level);
}

// NOLINTEND(cppcoreguidelines-pro-type-member-init)
    class Configuration
    {
        public:
            explicit Configuration(const std::span<char *> args);

            [[nodiscard]] bool get_help() const noexcept;
            [[nodiscard]] boost::log::trivial::severity_level
                get_log_level() const noexcept;
            [[nodiscard]] std::string get_bind_address() const noexcept;
            [[nodiscard]] uint16_t get_bind_port() const noexcept;
            [[nodiscard]] std::string get_address() const noexcept;
            [[nodiscard]] uint16_t get_port() const noexcept;
            [[nodiscard]] uint8_t get_ttl() const noexcept;
            [[nodiscard]] std::string get_interface_name() const noexcept;

            void apply_log_level() const;

        private:
            bool _help;
            boost::log::trivial::severity_level _log_level;
            std::string _bind_address;
            uint16_t _bind_port;
            std::string _address;
            uint16_t _port;
            uint8_t _ttl;
            std::string _interface_name;
    };

generates a clang-diagnostic-error:

>>> /usr/bin/clang-tidy --use-color -quiet -p /home/marek/git/mping-sender/build /home/marek/git/mping-sender/src/configuration.cpp
190715 warnings and 2 errors generated.
Error while processing /home/marek/git/mping-sender/src/configuration.cpp.
381430 warnings and 4 errors generated.
Error while processing /home/marek/git/mping-sender/src/configuration.cpp.
/usr/include/boost/type_traits/is_signed.hpp:37:25: error: in-class initializer for static data member is not a constant expression [clang-diagnostic-error]
   37 |    static const no_cv_t minus_one = (static_cast<no_cv_t>(-1));
      |                         ^           ~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/boost/type_traits/is_signed.hpp:45:60: note: in instantiation of template class 'boost::detail::is_signed_values<boost::log::trivial::severity_level>' requested here
   45 |    BOOST_STATIC_CONSTANT(bool, value = (!(::boost::detail::is_signed_values<T>::minus_one  > boost::detail::is_signed_values<T>::zero)));
      |                                                            ^
/usr/include/boost/config/detail/suffix.hpp:410:72: note: expanded from macro 'BOOST_STATIC_CONSTANT'
  410 | #     define BOOST_STATIC_CONSTANT(type, assignment) static const type assignment
      |                                                                        ^
/usr/include/boost/type_traits/is_signed.hpp:74:40: note: in instantiation of template class 'boost::detail::is_signed_helper<boost::log::trivial::severity_level>' requested here
   74 |    BOOST_STATIC_CONSTANT(bool, value = type::value);
      |                                        ^
/usr/include/boost/config/detail/suffix.hpp:410:72: note: expanded from macro 'BOOST_STATIC_CONSTANT'
  410 | #     define BOOST_STATIC_CONSTANT(type, assignment) static const type assignment
      |                                                                        ^
/usr/include/boost/type_traits/is_signed.hpp:79:85: note: in instantiation of template class 'boost::detail::is_signed_impl<boost::log::trivial::severity_level>' requested here
   79 | template <class T> struct is_signed : public integral_constant<bool, boost::detail::is_signed_impl<T>::value> {};
      |                                                                                     ^
/usr/include/boost/lexical_cast/detail/converter_lexical_streams.hpp:315:44: note: in instantiation of template class 'boost::is_signed<boost::log::trivial::severity_level>' requested here
  315 |         typename boost::enable_if_c<boost::is_signed<Type>::value && !boost::is_enum<Type>::value, bool>::type
      |                                            ^
/usr/include/boost/lexical_cast/detail/converter_lexical.hpp:449:67: note: while substituting deduced template arguments into function template 'stream_in' [with Type = boost::log::trivial::severity_level]
  449 |                 -> decltype(std::declval<optimized_src_stream&>().stream_in(std::declval<lcast::exact<T>>()), optimized_src_stream{});
      |                                                                   ^
/usr/include/boost/lexical_cast/detail/converter_lexical.hpp:454:46: note: while substituting explicitly-specified template arguments into function template 'detect_type' 
  454 |             using from_src_stream = decltype(detect_type<Source>(1));
      |                                              ^
/usr/include/boost/lexical_cast/try_lexical_convert.hpp:67:20: note: in instantiation of template class 'boost::detail::lexical_converter_impl<std::basic_string<char>, boost::log::trivial::severity_level>' requested here
   67 |             return caster_type::try_convert(arg, result);
      |                    ^
/usr/include/boost/lexical_cast.hpp:42:41: note: in instantiation of function template specialization 'boost::conversion::detail::try_lexical_convert<std::basic_string<char>, boost::log::trivial::severity_level>' requested here
   42 |         if (!boost::conversion::detail::try_lexical_convert(arg, result)) {
      |                                         ^
/usr/include/boost/program_options/value_semantic.hpp:202:46: note: in instantiation of function template specialization 'boost::lexical_cast<std::basic_string<char>, boost::log::trivial::severity_level>' requested here
  202 |             m_default_value_as_text = boost::lexical_cast<std::string>(v);
      |                                              ^
../src/configuration.cpp:16:108: note: in instantiation of member function 'boost::program_options::typed_value<boost::log::trivial::severity_level>::default_value' requested here
   16 |       ("log-level", boost::program_options::value<boost::log::trivial::severity_level>(&this->_log_level)->default_value(boost::log::trivial::info), "Log level. Valid options are trace, debug, info, warning, error and fatal. Defaults to info.")
      |                                                                                                            ^
/usr/include/boost/type_traits/is_signed.hpp:37:38: note: integer value -1 is outside the valid range of values [0, 7] for the enumeration type 'severity_level'
   37 |    static const no_cv_t minus_one = (static_cast<no_cv_t>(-1));
      |                                      ^
/usr/include/boost/type_traits/is_unsigned.hpp:38:25: error: in-class initializer for static data member is not a constant expression [clang-diagnostic-error]
   38 |    static const no_cv_t minus_one = (static_cast<no_cv_t>(-1));
      |                         ^           ~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/boost/type_traits/is_unsigned.hpp:45:58: note: in instantiation of template class 'boost::detail::is_unsigned_values<boost::log::trivial::severity_level>' requested here
   45 |    BOOST_STATIC_CONSTANT(bool, value = (::boost::detail::is_unsigned_values<T>::minus_one > ::boost::detail::is_unsigned_values<T>::zero));
      |                                                          ^
/usr/include/boost/config/detail/suffix.hpp:410:72: note: expanded from macro 'BOOST_STATIC_CONSTANT'
  410 | #     define BOOST_STATIC_CONSTANT(type, assignment) static const type assignment
      |                                                                        ^
/usr/include/boost/type_traits/is_unsigned.hpp:74:40: note: in instantiation of template class 'boost::detail::is_ununsigned_helper<boost::log::trivial::severity_level>' requested here
   74 |    BOOST_STATIC_CONSTANT(bool, value = type::value);
      |                                        ^
/usr/include/boost/config/detail/suffix.hpp:410:72: note: expanded from macro 'BOOST_STATIC_CONSTANT'
  410 | #     define BOOST_STATIC_CONSTANT(type, assignment) static const type assignment
      |                                                                        ^
/usr/include/boost/type_traits/is_unsigned.hpp:79:87: note: in instantiation of template class 'boost::detail::is_unsigned<boost::log::trivial::severity_level>' requested here
   79 | template <class T> struct is_unsigned : public integral_constant<bool, boost::detail::is_unsigned<T>::value> {};
      |                                                                                       ^
/usr/include/boost/lexical_cast/detail/converter_lexical_streams.hpp:319:44: note: in instantiation of template class 'boost::is_unsigned<boost::log::trivial::severity_level>' requested here
  319 |         typename boost::enable_if_c<boost::is_unsigned<Type>::value && !boost::is_enum<Type>::value, bool>::type
      |                                            ^
/usr/include/boost/lexical_cast/detail/converter_lexical.hpp:449:67: note: while substituting deduced template arguments into function template 'stream_in' [with Type = boost::log::trivial::severity_level]
  449 |                 -> decltype(std::declval<optimized_src_stream&>().stream_in(std::declval<lcast::exact<T>>()), optimized_src_stream{});
      |                                                                   ^
/usr/include/boost/lexical_cast/detail/converter_lexical.hpp:454:46: note: while substituting explicitly-specified template arguments into function template 'detect_type' 
  454 |             using from_src_stream = decltype(detect_type<Source>(1));
      |                                              ^
/usr/include/boost/lexical_cast/try_lexical_convert.hpp:67:20: note: in instantiation of template class 'boost::detail::lexical_converter_impl<std::basic_string<char>, boost::log::trivial::severity_level>' requested here
   67 |             return caster_type::try_convert(arg, result);
      |                    ^
/usr/include/boost/lexical_cast.hpp:42:41: note: in instantiation of function template specialization 'boost::conversion::detail::try_lexical_convert<std::basic_string<char>, boost::log::trivial::severity_level>' requested here
   42 |         if (!boost::conversion::detail::try_lexical_convert(arg, result)) {
      |                                         ^
/usr/include/boost/program_options/value_semantic.hpp:202:46: note: in instantiation of function template specialization 'boost::lexical_cast<std::basic_string<char>, boost::log::trivial::severity_level>' requested here
  202 |             m_default_value_as_text = boost::lexical_cast<std::string>(v);
      |                                              ^
../src/configuration.cpp:16:108: note: in instantiation of member function 'boost::program_options::typed_value<boost::log::trivial::severity_level>::default_value' requested here
   16 |       ("log-level", boost::program_options::value<boost::log::trivial::severity_level>(&this->_log_level)->default_value(boost::log::trivial::info), "Log level. Valid options are trace, debug, info, warning, error and fatal. Defaults to info.")
      |                                                                                                            ^
/usr/include/boost/type_traits/is_unsigned.hpp:38:38: note: integer value -1 is outside the valid range of values [0, 7] for the enumeration type 'severity_level'
   38 |    static const no_cv_t minus_one = (static_cast<no_cv_t>(-1));
      |        
Dominant language
HTML
Stars
8.6k
Forks
2k
Avg merge
9h 35m
Merged PRs (30d)
1

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

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/boost

All issues in boostorg/boost

Similar issues

More Compilers issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.