I stumbled upon constexpr template functions calling non constexpr functions: The next line of the fragment fails to compile, as expected, due to a call to non constexpr, but foo compiles. Can someone tell me the reason why compiling foo?
template<class T>
void set(T& x){
x++;
}
template<class T>
constexpr void foo(T& x){
set<T>(x);
}
constexpr void bar(int& x){
set<int>(x);
}
void bar(){
int x = 5;
foo(x);
bar(x);
}
The compiler cannot compile with an error:
<source>: In function 'constexpr void bar(int&)':
<source>:12:13: error: call to non-constexpr function 'void set(T&) [with T = int]'
set<int>(x);
~~~~~~~~^~~
Compiler returned: 1
Edit: Added compiler error and rehash question. Side effects are not concentrated here.
Bolov Rekete1111 , . constexpr , constexpr sem constexpr. -Og , constexpr foo , foo2 , constexpr (, of constexpr):
template<class T>
void set(volatile T& x){
x++;
}
template<class T>
constexpr void foo(T& x){
set<T>(x);
}
template<class T>
void foo2(T& x){
set<T>(x);
}
void bar(){
int x = 5;
foo(x);
foo2(x);
}
:
void set<int>(int volatile&):
ldr r3, [r0]
add r3, r3, #1
str r3, [r0]
bx lr
void foo2<int>(int&):
push {r4, lr}
bl void set<int>(int volatile&)
pop {r4, lr}
bx lr
bar():
push {r4, lr}
sub sp, sp, #8
add r4, sp, #8
mov r3, #5
str r3, [r4, #-4]!
mov r0, r4
bl void set<int>(int volatile&)
mov r0, r4
bl void foo2<int>(int&)
add sp, sp, #8
pop {r4, lr}
bx lr