www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Checking for CTFE at compile-time

reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
Opposite to run-time checking via

if (__ctfe)
{
     //
}

is there no way of checking at compile-time whether the current 
scope of a function is being executed in CTFE?
Nov 07 2018
parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Wed, Nov 07, 2018 at 10:01:15PM +0000, Per Nordl÷w via Digitalmars-d-learn
wrote:
 Opposite to run-time checking via
 
 if (__ctfe)
 {
     //
 }
It's actually not a "run-time" check, because the backend optimizer will optimize out the true branch (check the emitted asm to confirm this).
 is there no way of checking at compile-time whether the current scope
 of a function is being executed in CTFE?
`if (__ctfe)` *is* a compile-time check. I think you're getting confused by the blanket term "compile-time", that actually means (at least) two different things. See: https://wiki.dlang.org/User:Quickfur/Compile-time_vs._compile-time T -- Real men don't take backups. They put their source on a public FTP-server and let the world mirror it. -- Linus Torvalds
Nov 07 2018