Skip to content

allow to use reflection in constant time evaluation#8978

Merged
jtdavis777 merged 6 commits intogoogle:masterfrom
fdr400:patch-1
Mar 20, 2026
Merged

allow to use reflection in constant time evaluation#8978
jtdavis777 merged 6 commits intogoogle:masterfrom
fdr400:patch-1

Conversation

@fdr400
Copy link
Contributor

@fdr400 fdr400 commented Mar 13, 2026

It is useful for constexpr switch cases, for example, using magic_enum::enum_switch

Example from our code base:

::parquet::schema::NodePtr makeNode(const ::reflection::Field& field) {
  const auto name = field.name()->str();

  return magic_enum::enum_switch(
      [&name](auto val) -> ::parquet::schema::NodePtr {
        constexpr auto base_type = val;
        if constexpr (::reflection::IsScalar(base_type)) { // <- must be constexpr to be evaluated
          return makeScalarSchema<ToCppNativeType<base_type>>(name);
        } else if constexpr (base_type == ::reflection::BaseType::String){
          return makeStringSchema<std::string>(name);
        } else {
          static_assert(false);
        }
      },
      field.type()->base_type());
}

allow to use reflection in constant time evaluation
@fdr400 fdr400 requested a review from dbaileychess as a code owner March 13, 2026 09:04
@google-cla
Copy link

google-cla bot commented Mar 13, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@github-actions github-actions bot added the c++ label Mar 13, 2026
jtdavis777
jtdavis777 previously approved these changes Mar 19, 2026
@jtdavis777
Copy link
Collaborator

should we look at any other functions in this file to mark constexpr? We just need to keep C++11 compatible.

@fdr400
Copy link
Contributor Author

fdr400 commented Mar 19, 2026

@jtdavis777 thanks for review! Yes, it is possible. I made two more functions constexpr. I believe no more functions in the file can be converted to constexpr because of C++11 limitations

@fdr400 fdr400 requested a review from jtdavis777 March 19, 2026 17:57
@jtdavis777
Copy link
Collaborator

is it required to place the constexpr array outside the function for c++11? it's probably fine to expose that, but I'd prefer if we didn't

@fdr400
Copy link
Contributor Author

fdr400 commented Mar 19, 2026

@jtdavis777 unfortunately, yes(
In C++11 constexpr function may only have a single return statement

Alternative solution is

constexpr size_t GetTypeSize(reflection::BaseType base_type) {
  return
      base_type == reflection::None ? 0 :
      base_type == reflection::UType ? 1 :
      base_type == reflection::Bool ? 1 :
      base_type == reflection::Byte ? 1 :
      base_type == reflection::UByte ? 1 :
      base_type == reflection::Short ? 2 :
      base_type == reflection::UShort ? 2 :
      base_type == reflection::Int ? 4 :
      base_type == reflection::UInt ? 4 :
      base_type == reflection::Long ? 8 :
      base_type == reflection::ULong ? 8 :
      base_type == reflection::Float ? 4 :
      base_type == reflection::Double ? 8 :
      base_type == reflection::String ? 4 :
      base_type == reflection::Vector ? 4 :
      base_type == reflection::Obj ? 4 :
      base_type == reflection::Union ? 4 :
      base_type == reflection::Array ? 0 :
      base_type == reflection::Vector64 ? 8 :
      0; // MaxBaseType / fallback
}

but it looks a bit awkward, I guess..

@jtdavis777
Copy link
Collaborator

Yeah that is t worth it. I thinks it is good as is.

@jtdavis777 jtdavis777 enabled auto-merge (squash) March 20, 2026 01:57
@jtdavis777 jtdavis777 merged commit 8396e00 into google:master Mar 20, 2026
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants