Structured bindings do not work with `environment::key_value_pair[_view]`

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

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
52/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
cpp

Research direction

Start with boost/process/environment.hpp and the key_value_pair_view and tuple_size definitions referenced in boost/process/v2/environment.hpp. Reproduce the supplied bug.cpp with GCC and Clang; done means both const and non-const structured-binding examples compile successfully.

Written by the indexing model from the issue text.

Description

Structured bindings do not work with environment::key_value_pair and environment::key_value_pair_view. For example, the following code does not compile when using GCC 15.2.1 or Clang 21.1.6 on Linux:

#include <iostream>
#include <boost/process/environment.hpp>

int main() {
    for (const auto [name, value] : boost::process::environment::current())
        std::cout << name << '=' << value << '\n';
    return 0;
}
Compiler output
$ g++ bug.cpp -lboost_process
bug.cpp: In function ‘int main()’:
bug.cpp:5:21: error: cannot decompose inaccessible member ‘boost::process::v2::environment::key_value_pair_view::value_’ of ‘const boost::process::v2::environment::key_value_pair_view’
    5 |     for (const auto [name, value] : boost::process::environment::current())
      |                     ^~~~~~~~~~~~~
In file included from /usr/include/boost/process/environment.hpp:1,
                 from bug.cpp:2:
/usr/include/boost/process/v2/environment.hpp:435:20: note: declared private here
  435 |   string_view_type value_;
      |                    ^~~~~~
$ clang bug.cpp -lboost_process
bug.cpp:5:21: error: type 'const key_value_pair_view' decomposes into 1 element, but 2 names were
      provided
    5 |     for (const auto [name, value] : boost::process::environment::current())
      |                     ^
/usr/include/boost/process/v2/environment.hpp:1377:14: note: selected 'begin' function with
      iterator type 'iterator'
 1377 |     iterator begin() const {return iterator(handle_.get());}
      |              ^
1 error generated.

Interestingly, if const auto is replaced with just auto, GCC compiles the code without any warnings, while Clang still does not compile it but produces a different error message, that seems to explain the actual cause:

bug.cpp:5:15: error: 'value' is a private member of 'std::integral_constant<unsigned long, 2>'
    5 |     for (auto [name, value] : boost::process::environment::current())
      |               ^
/usr/include/boost/process/v2/environment.hpp:1280:82: note: constrained by implicitly private
      inheritance here
 1280 | class tuple_size<BOOST_PROCESS_V2_NAMESPACE::environment::key_value_pair_view> : integral_constant<std::size_t, 2u> {};
      |                                                                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib64/gcc/x86_64-suse-linux/15/../../../../include/c++/15/type_traits:94:28: note: 
      member is declared here
   94 |       static constexpr _Tp value = __v;
      |                            ^
1 error generated.
Dominant language
C++
Stars
145
Forks
151
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/process

All issues in boostorg/process

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.