这是一个问题,其原因对我来说很模糊,但幸运的是,解决方法很容易。
考虑以下代码(让我称其为 my main.cpp):
#include <algorithm>
struct Foo {
static constexpr float BAR = .42;
float operator()() const noexcept {
float zero = .0;
return std::min(zero, BAR);
}
};
int main() {
Foo foo;
foo();
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译它时,出现错误:
foobar:~/stackoverflow$ g++ -std=c++11 main.cpp
/tmp/ccjULTPy.o: 在函数 'Foo::operator()() const':
main.cpp:(.text._ZNK3FooclEv[_ZNK3FooclEv] +0x1a):对‘Foo::BAR’的未定义引用
collect2:错误:ld 返回 1 个退出状态
如果我使用以下语句,也会发生同样的情况(很明显):
return std::min(zero, Foo::BAR);
Run Code Online (Sandbox Code Playgroud)
下面是上面示例的稍微修改的版本。
这个编译没有错误,即使我仍然指的是BAR成员:
#include <algorithm>
struct Foo {
static constexpr float BAR = .42;
float operator()() const noexcept {
float zero = .0; …Run Code Online (Sandbox Code Playgroud) 我有几个问题
1)
#include <iostream>
template<typename T>
void func(T t){}
int main()
{
int i;
double d;
std::cin>>i;
if(i==1)
func(i);
else
func(d);
}
Run Code Online (Sandbox Code Playgroud)
何时(运行时/编译时)生成所需的函数?实例化后有多少个版本的函数?
2)有什么区别
template<typename T> auto func(T t){return 0;}
Run Code Online (Sandbox Code Playgroud)
和
template<typename T> constexpr auto func(T t){return 0;}
Run Code Online (Sandbox Code Playgroud)
据我了解,template在编译时constexpr也有效。为什么(以及何时)我需要使用 constexpr模板?
我有一些使用 msvc 编译但不在 gcc 下编译的代码。我想知道这是 msvc 的非标准功能还是 gcc 中的错误(或者更准确地说是 MinGW 的 v5.0.3 版本)
考虑以下代码:
template <class T>
struct object_with_func_ptr {
const T func; // An object to hold a const function pointer.
};
class foo {
public:
void bar() {} // The function I want to point to.
static constexpr auto get_bar() {
return object_with_func_ptr<decltype(&bar)>{&bar}; // A constexpr to wrap a function pointer.
}
};
template <class Func, Func Fn> struct template_using_func {};
int main() {
constexpr auto bar_obj = …Run Code Online (Sandbox Code Playgroud) struct A{
constexpr operator bool()const{ return true; }
};
int main(){
auto f = [](auto v){ if constexpr(v){} };
A a;
f(a);
}
Run Code Online (Sandbox Code Playgroud)
clang 6 接受代码,GCC 8 拒绝它:
$ g++ -std=c++17 main.cpp
main.cpp: In lambda function:
main.cpp:6:37: error: 'v' is not a constant expression
auto f = [](auto v){ if constexpr(v){} };
^
Run Code Online (Sandbox Code Playgroud)
谁是正确的,为什么?
当我根据每个引用获取参数时,都拒绝代码:
struct A{
constexpr operator bool()const{ return true; }
};
int main(){
auto f = [](auto& v){ if constexpr(v){} };
constexpr A a;
f(a);
}
Run Code Online (Sandbox Code Playgroud)
用clang …
我正在尝试编写一个 constexpr find 函数,该函数将返回包含某个值的 std::array 的索引。下面的函数似乎可以正常工作,除非包含的类型是const char*:
#include <array>
constexpr auto name1() {
return "name1";
}
constexpr auto name2() {
return "name2";
}
template <class X, class V>
constexpr auto find(X& x, V key) {
std::size_t i = 0;
while(i < x.size()) {
if(x[i] == key) return i;
++i;
}
return i;
}
int main() {
constexpr std::array<const char*, 2> x{{name1(), name2()}};
constexpr auto f1 = find(x, name1()); // this compiles
constexpr auto f2 = find(x, name2()); // …Run Code Online (Sandbox Code Playgroud) 我正在寻找可在 constexpr 中使用的 ::std::function。用例:我有一个函数,它将函数指针作为参数,第二个函数将 lambda 传递给第一个函数。两者在编译时都是完全可执行的,所以我想对它们进行 constexpr。例如:
template <class _Type>
class ConstexprFunctionPtr
{
private:
using Type = typename ::std::decay<_Type>::type;
const Type function;
public:
constexpr inline
ConstexprFunctionPtr(const Type f)
: function(f)
{ }
template <typename... Types>
constexpr inline
auto
operator() (Types... args)
const {
return function(args... );
}
};
constexpr inline
void
test()
{
ConstexprFunctionPtr<int(int)> test([](int i) -> int {
return i + 1;
});
int i = test(100);
ConstexprFunctionPtr<int(int)> test2([=](int i) -> int {
return i + 1;
});
i …Run Code Online (Sandbox Code Playgroud) c++ metaprogramming template-meta-programming constexpr c++11
我发现很难理解为什么以下结果会导致编译时计算。我已经阅读了this,this,this和更多关于stackoverflow的问题,这些问题告诉我以下代码(至少据我所知)由于while循环而不应在编译时计算(该代码仅是示例说明了题):
template< unsigned N >
constexpr unsigned isStringNice(const char (&arr)[N], unsigned pos = 0)
{
//we do not like the 'D' char :)
int currPos = 0;
while(currPos < N){
if(arr [currPos] == 'D'){
throw 1;
}
currPos ++;
}
return 1;
}
constexpr unsigned isIdxValid( unsigned idx, unsigned len){
return idx >= len? throw 1 : idx;
}
template< unsigned N >
constexpr char nth_char(const char (&arr)[N], unsigned pos){
return isStringNice(arr),isIdxValid(pos, …Run Code Online (Sandbox Code Playgroud) 我只是偶然发现了GCC和Clang在显式默认constexpr ctor和某些继承方面的以下区别...
template <typename T>
struct A {
constexpr A() = default;
T v;
};
struct B : A<int> {
constexpr B() = default;
};
Run Code Online (Sandbox Code Playgroud)
GCC立即拒绝该代码,而Clang允许实例化这两种类型的非constexpr版本。我的猜测是Clang可能是正确的,但我不确定100%...
我试图了解const double* const用作模板的内幕。我有一些非常基本的计算想要高效执行,但是我不知道c ++编译器是如何工作的(汇编代码是什么)。
这个想法是为一个函数创建一个模板,该函数将3个常量double用作模板参数,并将double用作参数。
constexpr double p1 = 1;
constexpr double p2 = 2;
constexpr double p3 = 3;
template <const double* const a,
const double* const b,
const double* const c>
inline double func(double value)
{
constexpr double d = *a - *b;
constexpr double e = *a - *c;
constexpr double ratio = d / e;
constexpr double remain = *c - *a * ratio;
return value * ratio + remain;
}
double func2(double c) …Run Code Online (Sandbox Code Playgroud) 我想知道是否constexpr std::tuple可以在编译时对a进行排序:
template<typename T>
struct A{ T val; }; // a constexpr-enabled class
constexpr auto t = std::make_tuple( A<int>{3}, A<float>{1.f}, A<double>{2.0});
constexpr auto s = sort(t, [](auto&& v){return v.val; });
static_assert(std::is_same_v<std::tuple<A<float>,
A<double>,
A<int>>,decltype(s)>, "Wups");
Run Code Online (Sandbox Code Playgroud)
这可能吗,这里需要哪些构建块(std::sort是constexpr)?
(据我所知)在运行时不可能做到这一点,因为在运行时无法确定排序元组的类型。如果构建所有排列的类型映射,我也看不到一种方法,实际排列仍然只在运行时才知道。
回答/sf/answers/3220421851/ 给出了一些提示,它应该在编译时工作,但是如何?
c++ ×10
constexpr ×10
c++11 ×4
c++17 ×3
gcc ×2
templates ×2
arrays ×1
c++20 ×1
double ×1
if-constexpr ×1
precompiled ×1
sorting ×1
static ×1
stdtuple ×1
visual-c++ ×1