« first day (571 days earlier)      last day (4378 days later) » 

12:00 AM
Yes, it is executed at runtime (if your syntax were correct)
if you mean inside a decltype, then it is not executed, just examined for types
but the template is instantiated
 
no I'm just asking if the compiler executes the call to foo()
 
The compiler will emit code to call foo yes
f<int>().foo()
 
ah ok
so is std::vector<int> vec an instantiation of the template or does the compiler do the instantiating?
 
the compiler instantiates the template
 
@MooingDuck uhm, it does :-(
 
12:03 AM
to create a new type from the template named vector<int>
 
void display( vector<wstring> const& patterns )
{
    copy(
        begin( patterns ), end( patterns ),
        ostream_iterator<wstring, wchar_t>( wcout, L", " )
        );
    wcout << endl;
}
 
@TonyTheLion a template instantiation does not emit code though
 
the only code that would be called there would be the constructor for vector<int>
the compiler does that at compile time (instantiate the template)
 
12:04 AM
You can force instantiations to be generated.
 
so it instantiates the template and then only emits the code necessary for the call?
 
@CatPlusPlus don't confuse him with offtopic things :)
 
I don't know what's ontopic.
 
@TonyTheLion just like struct x { }; doesn't emit code, vector<int> doesn't emit code
only x xinst or vector<int> v emits code (for the ctors)
if they have them
I love C++
 
but does struct x { void dowhatever { std::cout << "blah"; } }; emit code?
 
12:06 AM
No
 
just the declaration of a struct emits no code
 
wait...
 
Xeo
@SethCarnegie Atleast in optimized code.
 
12:06 AM
ah, but the dowhatever function in my struct emits code right?
 
@TonyTheLion when it is called, yes
 
Member definitions do emit code.
 
Well only if they're linked in
 
12:07 AM
Unless they are inlined.
 
But they're not executable statements
 
What.
 
class definitions are not executable
 
a function is an executable statement
afaik
ah, yes class definitions
 
12:08 AM
and a function definition is not executable either
 
the machine has no notion of a class or struct
 
I said member definitions, not class definitions.
 
that's clear to me
 
Well, member functions.
 
@TonyTheLion But the compiler does
 
12:08 AM
right, the compiler just translates from C++ to machine code
 
Function definition emits code for the function.
 
Not necessarily
 
now, when you write a template, the compiler basically creates a class or function that can be executed from the template, with the right types
 
That code might be inlined or eliminated, but that's beside the point.
 
@TonyTheLion it creates a thing that it can make other definitions from, yes
 
12:10 AM
It makes a template of sorts.
 
It's a definition of a definition
 
the function definition only emits code if called
 
Wonder why they called it a template.
 
@DeadMG thank you, exactly
 
it's meta
 
12:10 AM
@CatPlusPlus that meaning is easily forgotten though
 
int foo() { return 4; }
int main() {}
.file "test.cpp"
.text
.globl _Z3foov
.def _Z3foov; .scl 2; .type 32; .endef
.seh_proc _Z3foov
_Z3foov:
pushq %rbp
.seh_pushreg %rbp
movq %rsp, %rbp
.seh_setframe %rbp, 0
.seh_endprologue
movl $4, %eax
popq %rbp
ret
.seh_endproc
.def __main; .scl 2; .type 32; .endef
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
pushq %rbp
.seh_pushreg %rbp
movq %rsp, %rbp
subq $32, %rsp
.seh_stackalloc 32
.seh_setframe %rbp, 32
.seh_endprologue
call __main
movl $0, %eax
addq $32, %rsp
popq %rbp
Oh wait, WHAT'S THAT.
 
but things like the true_type trait, that has a static const bool value = true;, the compiler somehow interprets this when used, so it must "execute" it, or did I get it wrong?
 
@TonyTheLion the compiler emits code to initialise that variable before main is entered, yeah
 
(It's also emitted with optimisations if you're wondering).
Function definitions can only be eliminated during linking, and only in some cases.
 
And it does that for each instantiation of the template
 
12:13 AM
@CatPlusPlus but that's just the code to setup the stackframe of main and then just a main with no code. I don't think that foo() is in there though
 
@TonyTheLion Yeah, that _Z3foov thing is there just for kicks.
 
@CatPlusPlus oops, missed it
:(
 
final version i think... pastebin.com/MEfFEyW5
 
@SethCarnegie but doesn't it "evaluate" it, of sorts, like when you use the traits to do SFINAE?
 
12:15 AM
@CatPlusPlus you can use -masm=intel to get more (as I see it) readable assembly
 
Assembly itself is not relevant. The function definition is there, even though it's not called, that's my point.
 
@TonyTheLion the compiler evalutes true to get true if that's what you mean
 
Because you can't assume it won't be called until you link.
 
evaluate what, exactly
@CatPlusPlus thank you CatPlusPlus for your input
and output
 
@SethCarnegie yes, but the value true or false must mean something to the compiler to know that a false used like in SFINAE, would eliminate the function from the overload set
 
12:17 AM
It means false.
What else would it mean.
file_not_found?
 
@CatPlusPlus we are talking on a higher plane of existence
 
yea I know, but a compiler is not a machine that's executing code, it's just a compiler
 
false is always false.
 
@TonyTheLion for compilers, they do kinda have to execute code
 
#define false TRUE
now it's not
@SethCarnegie ah ok
 
12:18 AM
@TonyTheLion for instance when you have a template that calculates the fibonacci sequence or whatever
it has to do the arithmetic etc.
 
When a compiler parses false it inserts boolean_literal(false) in that place in AST.
 
@SethCarnegie yes, that's a good example, because it instantiates the templates and "executes" them or something...
it's magic, I guess
 
It knows both the type and the value.
 
@TonyTheLion C++ templates are a language in the language so yeah, it's complicated
Almost every time I read a C question I think of how it's not a problem in C++
...and vice versa
 
There's no vice versa.
 
12:21 AM
yeah there is
 
Like?
 
templates are not a problem in C
 
Lack of templates is a problem in C.
 
That is, when I read a question about templates in C++, in C you would not have that problem
you'd have some other problem, true, but not that one
The simplicity of C gives it a lack of the extra problems of C++ (but of course also the lack of extra solutions)
that's all I mean
 
C is primitive, not simple.
 
12:23 AM
It's both
(simple when compared to C++ that is)
 
@SethCarnegie There's nothing simple about C.
the language specification might be lighter, but that's just because the specification of every program written in it has to be that much heavier
yes, templates are a bitch, but not having templates is a billion times worse
 
@ScottW exactly
by the definition I am using
 
I thought I'd burden SO with my question
0
Q: C++ template instantiation, what exactly does the compiler do?

Tony The LionNow, I'm familiar with templates and I'm somewhat familiar with things like SFINAE, and I've been wondering what goes on when a template is instantiated by the compiler. When you do things in TMP, like SFINAE, or even the simple Fibonacci sequence in TMP, it seems like the compiler is doing more...

and give the repwhores a chance :)
 
@Scott yeah, that's another facet of the word
@TonyTheLion I'm not sure what confuses you though
If the compiler sees you use as a template parameter a compile-time constant expression, it evaluates it
it has to to instantiate the template
 
meh, maybe I just confused myself
wouldn't be the first time
 
12:27 AM
@DeadMG maybe "simplistic" is a better word?
 
@DeadMG I think that's the truest thing I've heard in a while
 
I like "primitive"
 
That implies simple to me though
 
Yeah, but in a derogatory form.
 
I don't feel derogatory about cavemen and they were primitive
 
12:34 AM
@SethCarnegie Nah.
C can only be simplistic if writing a program in C is simplistic
since it is not, then C cannot be simplistic.
 
I can't wait to be done with Java.
 
who can blame you? it's a giant pile of suck
 
I didn't know "suck" came in piles
:P
 
amazing, isn't it?
but Java can make anything happen when it comes to suck
 
hahah
 
12:49 AM
It never ceases to amaze.
 
I've been lucky enough never to have to have been exposed to Java
but my luck might not be forever
 
They're so big on inheritance hierarchies to hide implementation details and enforce ideas about what a child class can be, but their own standard library goes against all that.
 
yea well, it's all OOP wankery
 
The Collection framework, which is like C++'s STL, has child classes throw exceptions for operations they don't support.
 
and fuck everything about OOP wankery
@Maxpm dafuq? But I think .NET has something like that too, no?
 
12:51 AM
@TonyTheLion I've never looked into .NET.
 
Though, .NET seems really silly to me on principle.
It's Java, but on fewer platforms. Which is, like, the whole point of Java.
 
Xeo
@TonyTheLion That's for your own stuff though, I think
 
lol, just MS copying others
 
Xeo
For when you write code
 
12:53 AM
it's just a strange idea to throw an exception for that
 
The example seems to imply it's used as a replacement for //TODO comments.
 
I'd rather have //TODO then
 
Yeah.
 
OO should be optional
 
OO is being overused
 
Xeo
12:54 AM
@TonyTheLion What about indev builds of libraries?
 
@TonyTheLion absolutely
 
and sometimes it makes things unnecessarily complex
 
Xeo
You want people to be able to code against an API, somewhat
 
it has it's place, just like functional programming
 
in the name of "information hiding"
@Xeo you can make nice API's with OO in it, only when it is needed
 
12:55 AM
OO is very good for code organisation though I think
 
@Xeo I don't see how it would be useful. If a function has yet to be implemented, it's that function's responsibility to supply dummy values.
 
@SethCarnegie but it's easily overdone
 
Xeo
@TonyTheLion I was talking about the exception
 
oh lol
 
Who was it that said "OO's lasting contribution is encapsulation" or something
 
12:56 AM
@Xeo not sure about the exception, is it really a circumstance which is "exceptional", that is the question we need to ask
 
yeah
 
does it warrant "exceptional circumstance"
 
it's a DeveloperStupidityException
2
 
I have a question about type traits in C++:
 
Xeo
lol
 
12:57 AM
used for when the developer was of exceptional stupidity
 
I never thought exceptions should have Exception in the name
 
Xeo
I hate that
 
@DeadMG I wish we had that one, to throw in a developers face :P
 
and inherited an interface he couldn't implement, because he was a moron
 
Xeo
12:57 AM
@DeadMG Who's talking about interfaces?
 
What I've read about type traits seems to say that they are used to hide implementation details.
 
@TonyTheLion struct DeveloperStupidityException { private: DeveloperStupidityException() { } }; there you go
@Maxpm Uh, I don't think so
 
@Maxpm Type traits are nothing but compile-time reflection
 
in the most gimped fashion you can imagine, which should not surprise anyone, as it's C++
 
12:58 AM
yup
 
Xeo
@DeadMG I wouldn't say reflection but introspection
 
so today I got given a new reason to repwhore
the guy that interviewed me at FB said I should keep working on my rep by answering questions :)
he actually asked me what my SO rep was
 

« first day (571 days earlier)      last day (4378 days later) »