[PATCH] D36353: Instantiate constexpr function when it is used

Serge Pavlov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Aug 5 06:01:04 PDT 2017


sepavloff created this revision.

Previous behavior was to instantiate constexpr function always, even if
it is used in the body of a template. For instance, in the following code:

  template<typename T> constexpr T foo(T x) { return x; }
  template<typename T> T bar(T x) { return foo(0); }

the function template specialization foo<int> was instantiated because it
was used in the body of template bar<T>. It is a violation of C++ Standard
requirement that an implementation shall not implicitly instantiate a
function template unless such instantiation is required ([temp.inst]p8).
This behavior is different from the case of non-constexpr function, which
indeed are instantiated when needed.

With this change constexpr functions are instantiated in the same way as
non-constexpr functions - when they are actually used. To make it possible
constant evaluator must be able to instantiate function definition. It is
provided by ASTContext, which in turn make the instantiation with help of
Sema.

This change fixes PR33561.


https://reviews.llvm.org/D36353

Files:
  include/clang/AST/ASTContext.h
  lib/AST/ASTContext.cpp
  lib/AST/ExprConstant.cpp
  lib/Sema/Sema.cpp
  lib/Sema/SemaExpr.cpp
  test/SemaTemplate/instantiate-constexpr-function.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D36353.109864.patch
Type: text/x-patch
Size: 8421 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170805/82fb6881/attachment-0001.bin>


More information about the cfe-commits mailing list