相关疑难解决方法(0)

未定义的对静态constexpr char []的引用

我想static const char在班上有一个数组.海湾合作委员会抱怨并告诉我应该使用constexpr,虽然现在它告诉我这是一个未定义的参考.如果我使数组成为非成员,那么它将编译.到底是怎么回事?

// .hpp
struct foo {
  void bar();
  static constexpr char baz[] = "quz";
};

// .cpp
void foo::bar() {
  std::string str(baz); // undefined reference to baz
}
Run Code Online (Sandbox Code Playgroud)

c++ static-members constexpr c++11

168
推荐指数
5
解决办法
4万
查看次数

理解左值到右值转换的示例

我很难理解这段代码(C++ 14草案标准[conv.lval]中的一个例子)是如何调用未定义的行为的g(false).为什么constexpr让程序有效?

另外,"不访问y.n" 是什么意思?在两个调用中g()我们都返回n数据成员,为什么最后一行说它不访问它?

struct S { int n; };
auto f() {
    S x { 1 };
    constexpr S y { 2 };
    return [&](bool b) { return (b ? y : x).n; };
}
auto g = f();
int m = g(false); // undefined behavior due to access of x.n outside its
                  // lifetime
int n = g(true);  // OK, does not access y.n
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer lvalue-to-rvalue constexpr c++14

4
推荐指数
1
解决办法
612
查看次数

静态 constexpr 成员似乎不符合 std::min

这是一个问题,其原因对我来说很模糊,但幸运的是,解决方法很容易。

考虑以下代码(让我称其为 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)

当我尝试编译它时,出现错误:

foob​​ar:~/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)

c++ static stl-algorithm constexpr c++11

4
推荐指数
1
解决办法
322
查看次数