【发布时间】:2017-02-26 03:26:28
【问题描述】:
header.h
extern constexpr double sqrt_of_2;
extern constexpr double sqrt_of_1_2;
double sqrt(double x);
main.cpp
#include <header.h>
int main() {
int n;
scanf("%d", &n);
printf("%lf %lf\n", sqrt_of_2, sqrt(n));
return 0;
}
source.cpp
#include <header.h>
double sqrt(double x) {
// complex bits of math
// huge function
// must not be in header for speedy compilation
// will call other small non-constexpr functions in this file
}
constexpr double sqrt_of_2 = sqrt(2.0);
constexpr double sqrt_of_1_2 = sqrt(0.5)
这显然行不通。
我无法在 source.cpp 中为 sqrt 添加 constexpr,因为这与 header.h 中的声明不匹配。我也不能在 header.h 中为sqrt 添加constexpr,因为constexpr 意味着inline,然后我需要将 source.cpp 中的所有内容传输到 header.h。
这可能吗?
【问题讨论】: