[PATCH] c++: Add std::to_underlying to the set of stdlib functions that are always folded

Jason Merrill jason@redhat.com
Tue May 13 20:23:27 GMT 2025


On 5/12/25 4:32 PM, Ville Voutilainen wrote:
> This function is yet another stdlib function that is just a simple cast, so having
> it appear while debugging is arguably not useful. So add it to the existing handling
> that always-folds some stdlib functions.
> 
> Add std::to_underlying to the set of stdlib functions that are always folded
> 
> gcc/cp/ChangeLog:
>          * cp-gimplify.cc (cp_fold): Add to_underlying.
> 
> gcc/testsuite/ChangeLog:
>          * g++.dg/opt/pr96780_cpp23.C: New.
> 
> libstdc++/ChangeLog:
>          * include/std/utility (to_underlying): Add the __always_inline__ attribute.

>  	    /* Check that the return and argument types are sane before
>  	       folding.  */
> -	    if (INDIRECT_TYPE_P (TREE_TYPE (x))
> -		&& INDIRECT_TYPE_P (TREE_TYPE (r)))
> +	    if (id_equal (DECL_NAME (callee), "to_underlying")
> +		|| (INDIRECT_TYPE_P (TREE_TYPE (x))
> +		    && INDIRECT_TYPE_P (TREE_TYPE (r))))

Maybe instead of INDIRECT_TYPE_P, check ! AGGREGATE_TYPE_P ?  I'm not 
sure how much sanity checking we need here, maybe we should just trust 
the library.

> +#include <utility>
> +
> +enum class A : char {a};
> +
> +extern A& x;
> +
> +void f() {
> +  auto&& x1 = std::to_underlying(x);
> +}
> +
> +// { dg-final { scan-tree-dump-not "= std::move" "gimple" } }
> +// { dg-final { scan-tree-dump-not "= std::forward" "gimple" } }
> +// { dg-final { scan-tree-dump-not "= std::addressof" "gimple" } }
> +// { dg-final { scan-tree-dump-not "= std::__addressof" "gimple" } }
> +// { dg-final { scan-tree-dump-not "= std::as_const" "gimple" } }
> +// { dg-final { scan-tree-dump-not "= std::forward_like" "gimple" } }
> +// { dg-final { scan-tree-dump-not "= std::to_underlying" "gimple" } }

Why check for a lot of things that aren't used in the testcase?

Jason



More information about the Libstdc++ mailing list