A performance comparison of std::variant and virtual dispatch reports that std::variant was 28% slower on GCC 11 and 50% faster on GCC 12. According to the preview, only the compiler changed.
The result shows that a dispatch choice does not have universally fixed performance: the compiler and standard-library implementation can affect measurements. The comparison does not establish that either method will always be faster in other programs or environments.
To evaluate your case, keep the workload, compiler options, and environment consistent while comparing alternatives. Record compiler and standard-library versions, repeat the measurements, and compare results before deciding.
If you use AI to analyze code, benchmarks, or internal documentation, send only what is necessary and follow your organization's policy for personal and confidential data. Do not include credentials or customer data without authorization.