Skip to content

Compilation Error: Cannot assign to non-static data member 'length' in document.h #2367

Open

Description

@bnjmnjrk

Description

When compiling tutorial.cpp, the build fails due to a const assignment error in rapidjson/document.h

Reproduce

wget https://github.com/Tencent/rapidjson/archive/refs/tags/v1.1.0.tar.gz
tar -xf v1.1.0.tar.gz
mv rapidjson-1.1.0/include/rapidjson .
mv rapidjson-1.1.0/example/tutorial/tutorial.cpp .
zig c++ tutorial.cpp --std=c++17

Error

Click to expand!
In file included from tutorial.cpp:4:
./rapidjson/document.h:319:82: error: cannot assign to non-static data member 'length' with const-qualified type 'const SizeType' (aka 'const unsigned int')
  319 |     GenericStringRef& operator=(const GenericStringRef& rhs) { s = rhs.s; length = rhs.length; }
      |                                                                           ~~~~~~ ^
./rapidjson/document.h:325:20: note: non-static data member 'length' declared const here
  325 |     const SizeType length; //!< length of the string (excluding the trailing NULL terminator)
      |     ~~~~~~~~~~~~~~~^~~~~~
./rapidjson/document.h:102:19: warning: 'iterator<std::random_access_iterator_tag, rapidjson::GenericMember<rapidjson::UTF8<>, rapidjson::MemoryPoolAllocator<>>>' is deprecated [-Wdeprecated-declarations]
  102 |     : public std::iterator<std::random_access_iterator_tag
      |                   ^
./rapidjson/document.h:548:22: note: in instantiation of template class 'rapidjson::GenericMemberIterator<false, rapidjson::UTF8<>, rapidjson::MemoryPoolAllocator<>>' requested here
  548 |     typedef typename GenericMemberIterator<false,Encoding,Allocator>::Iterator MemberIterator;  //!< Member iterator for iterating in object.
      |                      ^
./rapidjson/document.h:2024:32: note: in instantiation of template class 'rapidjson::GenericValue<rapidjson::UTF8<>>' requested here
 2024 | class GenericDocument : public GenericValue<Encoding, Allocator> {
      |                                ^
tutorial.cpp:18:14: note: in instantiation of template class 'rapidjson::GenericDocument<rapidjson::UTF8<>>' requested here
   18 |     Document document;  // Default template parameter uses UTF8 and MemoryPoolAllocator.
      |              ^
zig-x86_64-linux-0.15.2/lib/libcxx/include/__iterator/iterator.h:23:29: note: 'iterator<std::random_access_iterator_tag, rapidjson::GenericMember<rapidjson::UTF8<>, rapidjson::MemoryPoolAllocator<>>>' has been explicitly marked deprecated here
   23 | struct _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 iterator {
      |                             ^
zig-x86_64-linux-0.15.2/lib/libcxx/include/__config:725:41: note: expanded from macro '_LIBCPP_DEPRECATED_IN_CXX17'
  725 | #    define _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_DEPRECATED
      |                                         ^
zig-x86_64-linux-0.15.2/lib/libcxx/include/__config:698:49: note: expanded from macro '_LIBCPP_DEPRECATED'
  698 | #      define _LIBCPP_DEPRECATED __attribute__((__deprecated__))
      |                                                 ^
In file included from tutorial.cpp:4:
./rapidjson/document.h:110:18: warning: 'iterator<std::random_access_iterator_tag, rapidjson::GenericMember<rapidjson::UTF8<>, rapidjson::MemoryPoolAllocator<>>>' is deprecated [-Wdeprecated-declarations]
  110 |     typedef std::iterator<std::random_access_iterator_tag,ValueType> BaseType;
      |                  ^
zig-x86_64-linux-0.15.2/lib/libcxx/include/__iterator/iterator.h:23:29: note: 'iterator<std::random_access_iterator_tag, rapidjson::GenericMember<rapidjson::UTF8<>, rapidjson::MemoryPoolAllocator<>>>' has been explicitly marked deprecated here
   23 | struct _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 iterator {
      |                             ^
zig-x86_64-linux-0.15.2/lib/libcxx/include/__config:725:41: note: expanded from macro '_LIBCPP_DEPRECATED_IN_CXX17'
  725 | #    define _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_DEPRECATED
      |                                         ^
zig-x86_64-linux-0.15.2/lib/libcxx/include/__config:698:49: note: expanded from macro '_LIBCPP_DEPRECATED'
  698 | #      define _LIBCPP_DEPRECATED __attribute__((__deprecated__))
      |                                                 ^
In file included from tutorial.cpp:4:
./rapidjson/document.h:102:19: warning: 'iterator<std::random_access_iterator_tag, const rapidjson::GenericMember<rapidjson::UTF8<>, rapidjson::MemoryPoolAllocator<>>>' is deprecated [-Wdeprecated-declarations]
  102 |     : public std::iterator<std::random_access_iterator_tag
      |                   ^
./rapidjson/document.h:549:22: note: in instantiation of template class 'rapidjson::GenericMemberIterator<true, rapidjson::UTF8<>, rapidjson::MemoryPoolAllocator<>>' requested here
  549 |     typedef typename GenericMemberIterator<true,Encoding,Allocator>::Iterator ConstMemberIterator;  //!< Constant member iterator for iterating in object.
      |                      ^
./rapidjson/document.h:2024:32: note: in instantiation of template class 'rapidjson::GenericValue<rapidjson::UTF8<>>' requested here
 2024 | class GenericDocument : public GenericValue<Encoding, Allocator> {
      |                                ^
tutorial.cpp:18:14: note: in instantiation of template class 'rapidjson::GenericDocument<rapidjson::UTF8<>>' requested here
   18 |     Document document;  // Default template parameter uses UTF8 and MemoryPoolAllocator.
      |              ^
zig-x86_64-linux-0.15.2/lib/libcxx/include/__iterator/iterator.h:23:29: note: 'iterator<std::random_access_iterator_tag, const rapidjson::GenericMember<rapidjson::UTF8<>, rapidjson::MemoryPoolAllocator<>>>' has been explicitly marked deprecated here
   23 | struct _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 iterator {
      |                             ^
zig-x86_64-linux-0.15.2/lib/libcxx/include/__config:725:41: note: expanded from macro '_LIBCPP_DEPRECATED_IN_CXX17'
  725 | #    define _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_DEPRECATED
      |                                         ^
zig-x86_64-linux-0.15.2/lib/libcxx/include/__config:698:49: note: expanded from macro '_LIBCPP_DEPRECATED'
  698 | #      define _LIBCPP_DEPRECATED __attribute__((__deprecated__))
      |                                                 ^
In file included from tutorial.cpp:4:
./rapidjson/document.h:110:18: warning: 'iterator<std::random_access_iterator_tag, const rapidjson::GenericMember<rapidjson::UTF8<>, rapidjson::MemoryPoolAllocator<>>>' is deprecated [-Wdeprecated-declarations]
  110 |     typedef std::iterator<std::random_access_iterator_tag,ValueType> BaseType;
      |                  ^
zig-x86_64-linux-0.15.2/lib/libcxx/include/__iterator/iterator.h:23:29: note: 'iterator<std::random_access_iterator_tag, const rapidjson::GenericMember<rapidjson::UTF8<>, rapidjson::MemoryPoolAllocator<>>>' has been explicitly marked deprecated here
   23 | struct _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 iterator {
      |                             ^
zig-x86_64-linux-0.15.2/lib/libcxx/include/__config:725:41: note: expanded from macro '_LIBCPP_DEPRECATED_IN_CXX17'
  725 | #    define _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_DEPRECATED
      |                                         ^
zig-x86_64-linux-0.15.2/lib/libcxx/include/__config:698:49: note: expanded from macro '_LIBCPP_DEPRECATED'
  698 | #      define _LIBCPP_DEPRECATED __attribute__((__deprecated__))
      |                                                 ^
In file included from tutorial.cpp:4:
./rapidjson/document.h:1952:25: warning: first argument in call to 'memcpy' is a pointer to non-trivially copyable type 'Member' (aka 'GenericMember<rapidjson::UTF8<char>, rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>>') [-Wnontrivial-memcall]
 1952 |             std::memcpy(m, members, count * sizeof(Member));
      |                         ^
./rapidjson/document.h:2363:43: note: in instantiation of member function 'rapidjson::GenericValue<rapidjson::UTF8<>>::SetObjectRaw' requested here
 2363 |         stack_.template Top<ValueType>()->SetObjectRaw(members, memberCount, GetAllocator());
      |                                           ^
./rapidjson/reader.h:1736:31: note: in instantiation of member function 'rapidjson::GenericDocument<rapidjson::UTF8<>>::EndObject' requested here
 1736 |             bool hr = handler.EndObject(c);
      |                               ^
./rapidjson/reader.h:1832:39: note: in instantiation of function template specialization 'rapidjson::GenericReader<rapidjson::UTF8<>, rapidjson::UTF8<>>::Transit<1U, rapidjson::GenericInsituStringStream<rapidjson::UTF8<>>, rapidjson::GenericDocument<rapidjson::UTF8<>>>' requested here
 1832 |             IterativeParsingState d = Transit<parseFlags>(state, t, n, is, handler);
      |                                       ^
./rapidjson/reader.h:487:20: note: in instantiation of function template specialization 'rapidjson::GenericReader<rapidjson::UTF8<>, rapidjson::UTF8<>>::IterativeParse<1U, rapidjson::GenericInsituStringStream<rapidjson::UTF8<>>, rapidjson::GenericDocument<rapidjson::UTF8<>>>' requested here
  487 |             return IterativeParse<parseFlags>(is, handler);
      |                    ^
./rapidjson/document.h:2159:40: note: in instantiation of function template specialization 'rapidjson::GenericReader<rapidjson::UTF8<>, rapidjson::UTF8<>>::Parse<1U, rapidjson::GenericInsituStringStream<rapidjson::UTF8<>>, rapidjson::GenericDocument<rapidjson::UTF8<>>>' requested here
 2159 |         parseResult_ = reader.template Parse<parseFlags>(is, *this);
      |                                        ^
./rapidjson/document.h:2175:16: note: in instantiation of function template specialization 'rapidjson::GenericDocument<rapidjson::UTF8<>>::ParseStream<1U, rapidjson::UTF8<>, rapidjson::GenericInsituStringStream<rapidjson::UTF8<>>>' requested here
 2175 |         return ParseStream<parseFlags, Encoding, InputStream>(is);
      |                ^
./rapidjson/document.h:2200:16: note: in instantiation of function template specialization 'rapidjson::GenericDocument<rapidjson::UTF8<>>::ParseStream<1U, rapidjson::GenericInsituStringStream<rapidjson::UTF8<>>>' requested here
 2200 |         return ParseStream<parseFlags | kParseInsituFlag>(s);
      |                ^
./rapidjson/document.h:2208:16: note: in instantiation of function template specialization 'rapidjson::GenericDocument<rapidjson::UTF8<>>::ParseInsitu<0U>' requested here
 2208 |         return ParseInsitu<kParseDefaultFlags>(str);
      |                ^
tutorial.cpp:28:18: note: in instantiation of member function 'rapidjson::GenericDocument<rapidjson::UTF8<>>::ParseInsitu' requested here
   28 |     if (document.ParseInsitu(buffer).HasParseError())
      |                  ^
./rapidjson/document.h:1952:25: note: explicitly cast the pointer to silence this warning
 1952 |             std::memcpy(m, members, count * sizeof(Member));
      |                         ^
      |                         (void*)
./rapidjson/document.h:1939:25: warning: first argument in call to 'memcpy' is a pointer to non-trivially copyable type 'GenericValue<UTF8<>>' [-Wnontrivial-memcall]
 1939 |             std::memcpy(e, values, count * sizeof(GenericValue));
      |                         ^
./rapidjson/document.h:2371:43: note: in instantiation of member function 'rapidjson::GenericValue<rapidjson::UTF8<>>::SetArrayRaw' requested here
 2371 |         stack_.template Top<ValueType>()->SetArrayRaw(elements, elementCount, GetAllocator());
      |                                           ^
./rapidjson/reader.h:1766:31: note: in instantiation of member function 'rapidjson::GenericDocument<rapidjson::UTF8<>>::EndArray' requested here
 1766 |             bool hr = handler.EndArray(c);
      |                               ^
./rapidjson/reader.h:1832:39: note: in instantiation of function template specialization 'rapidjson::GenericReader<rapidjson::UTF8<>, rapidjson::UTF8<>>::Transit<1U, rapidjson::GenericInsituStringStream<rapidjson::UTF8<>>, rapidjson::GenericDocument<rapidjson::UTF8<>>>' requested here
 1832 |             IterativeParsingState d = Transit<parseFlags>(state, t, n, is, handler);
      |                                       ^
./rapidjson/reader.h:487:20: note: in instantiation of function template specialization 'rapidjson::GenericReader<rapidjson::UTF8<>, rapidjson::UTF8<>>::IterativeParse<1U, rapidjson::GenericInsituStringStream<rapidjson::UTF8<>>, rapidjson::GenericDocument<rapidjson::UTF8<>>>' requested here
  487 |             return IterativeParse<parseFlags>(is, handler);
      |                    ^
./rapidjson/document.h:2159:40: note: in instantiation of function template specialization 'rapidjson::GenericReader<rapidjson::UTF8<>, rapidjson::UTF8<>>::Parse<1U, rapidjson::GenericInsituStringStream<rapidjson::UTF8<>>, rapidjson::GenericDocument<rapidjson::UTF8<>>>' requested here
 2159 |         parseResult_ = reader.template Parse<parseFlags>(is, *this);
      |                                        ^
./rapidjson/document.h:2175:16: note: in instantiation of function template specialization 'rapidjson::GenericDocument<rapidjson::UTF8<>>::ParseStream<1U, rapidjson::UTF8<>, rapidjson::GenericInsituStringStream<rapidjson::UTF8<>>>' requested here
 2175 |         return ParseStream<parseFlags, Encoding, InputStream>(is);
      |                ^
./rapidjson/document.h:2200:16: note: in instantiation of function template specialization 'rapidjson::GenericDocument<rapidjson::UTF8<>>::ParseStream<1U, rapidjson::GenericInsituStringStream<rapidjson::UTF8<>>>' requested here
 2200 |         return ParseStream<parseFlags | kParseInsituFlag>(s);
      |                ^
./rapidjson/document.h:2208:16: note: in instantiation of function template specialization 'rapidjson::GenericDocument<rapidjson::UTF8<>>::ParseInsitu<0U>' requested here
 2208 |         return ParseInsitu<kParseDefaultFlags>(str);
      |                ^
tutorial.cpp:28:18: note: in instantiation of member function 'rapidjson::GenericDocument<rapidjson::UTF8<>>::ParseInsitu' requested here
   28 |     if (document.ParseInsitu(buffer).HasParseError())
      |                  ^
./rapidjson/document.h:1939:25: note: explicitly cast the pointer to silence this warning
 1939 |             std::memcpy(e, values, count * sizeof(GenericValue));
      |                         ^
      |                         (void*)
6 warnings and 1 error generated.

Activity

  1. diamante0018 commented on Mar 18, 2026

    @diamante0018

    I upvoted this because I was able to replicate this with clang version 20

    clang version 18 and GCC 13 seems to not have this warning yet

  2. kev82 commented on Mar 28, 2026

    @kev82

    I just encountered this myself, it looks to have been fixed quite a while ago in this issue:

    #718

    Taking the latest master, I was able to build/run the tutorial with gcc 14 in debian trixie

    git clone https://github.com/Tencent/rapidjson.git
    cd rapidjson/
    g++ -std=c++17 -o tutorial example/tutorial/tutorial.cpp -I include
    ./tutorial
    
  3. diamante0018 commented on Apr 1, 2026

    @diamante0018

    That issue is from 2016, does that mean that us using rapidjson from the debian/ubuntu apt repo have been stuck on a 10 year old version?

    Is Tencent responsible for maintaining the apt repos? I don't think so... because that's how i replicated this issue... was using the apt rapidjson dev library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions