Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are a number of things that make this very difficult to do, almost certainly impossible.</p> <p>First, you have to understand that there is a difference between what <em>can be known</em> at compile-time, what is actually known at compile-time by a <em>specific</em> compiler, what information the standard <em>requires</em> that a compiler should track, and what information you can actually <em>extract and use</em> at compile-time (and in what context you can use that information). I think that what you are asking is theoretically possible (under some assumptions, for instance, that everything happens in a single translation-unit). But I am almost certain the standard does not require that a compiler be able to give you that kind of information, let alone be required to know that kind of information itself. And I also doubt that most compilers would be able to know that information in all but the most trivial cases.</p> <p>The reason for this is simple. Your question really boils down to asking the compiler if function A has been called anywhere prior to point B. Where function A is your function template and point B is the point of instantiation and first creation of your class template. This means that when the compiler reaches point B it must have analyzed all the code (and instantiated any templates) that would correspond to possible execution points prior to point B. In other words, it would require that the compiler must follow the path of execution throughout when compiling. Compilers don't do this. The function template could easily be buried in some other function that the compiler will not get to compiling before reaching point B even though that function will be executed before creating the class-template object. And the converse could be true too, i.e., the compiler has instantiated the function template, even though it is called after the class-template appears.</p> <p>Then, there is the problem of branches. Instantiating a function template does not mean that it will be executed, due to some conditional statement or an exception, or even a go-to for that matter. The analysis done by the compiler would have to be pretty insane in order to tell you with certainty whether the function will have been executed for sure by the time you reach the constructor of the class template.</p> <p>Then, there is the problem of translation units. What if the function template instantiation and the class template instantiation do not appear in the same translation unit? Even if you had a working mechanism to detect that the function template was instantiated, it wouldn't work in this case even if the execution order was correct.</p> <p>Long story short, use a run-time check, or find a way to call the function from the constructor (maybe with default parameters and a debug warning message at run-time).</p> <p><strong>EDIT:</strong> Generally, the way to ensure, at compile-time, that a certain order of function-calls / constructor-calls is to tie them to a chain of initializations. A simple trick is to make the class template only constructible through calling a member function of another class (e.g., on a global / singleton object of that class), and make that object only constructible through calling the function template with the required parameters. There are a number of similar tricks, often involving static local variables as "initialize once" variables. But of course, these make the code a bit more weird.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload