C++ Logo

std-discussion

Advanced search

Re: constexpr functions and variables

From: Federico Kircheis <federico_at_[hidden]>
Date: Thu, 4 Apr 2024 09:23:10 +0200
On Wed, Apr 03, 2024 at 11:26:51AM -0700, Thiago Macieira via Std-Discussion wrote:
>On Wednesday 3 April 2024 05:52:43 PDT Federico Kircheis via Std-Discussion
>wrote:
>> The question is if something similar was considered for functions,
>> because there are similar advantages
>
>constexpr functions are implicitly inline. Therefore, you MUST see the body in
>order to call them. We don't have extern inline like C does. It seems to me
>that to have what you're requesting, this is what you need to focus on first.
>
>I mean, extern inlines compile, but they does not do what they do in C, which
>is to emit an out-of-line non-weak/non-mergeable copy of the function. Compare
>https://c.godbolt.org/z/Y4xebq4PE to https://gcc.godbolt.org/z/cx5qorn5T
>

Oh, this is a good point

// lib0.hpp
const char* foo(int);

// lib0.cpp
inline const char* foo(int){
 return "hello world!";
}

// main.cpp
#include "lib0.hpp"

#include <cstdio>

int main(){
 std::puts(foo(42));
}

fails to compile (and succeeds when inline is removed from lib0.cpp)

https://godbolt.org/z/EPzhdncMe

Received on 2024-04-04 07:23:18