本文介绍了如何声明constexpr extern?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以声明变量 extern constexpr 并将其定义在另一个文件中?

Is it possible to declare a variable extern constexpr and define it in another file?

我尝试过但是编译器给出错误:

I tried it but the compiler gives error:

在.h中:

extern constexpr int i;

in .cpp:

constexpr int i = 10;


推荐答案

不行,这就是标准说(第7.1.5节):

no you can't do it, here's what the standard says (section 7.1.5):

标准提供的一些示例:

  constexpr void square(int &x);  // OK: declaration
  constexpr int bufsz = 1024;  // OK: definition
  constexpr struct pixel {  // error: pixel is a type
    int x;
    int y;
    constexpr pixel(int);  // OK: declaration
  };

  extern constexpr int memsz; // error: not a definition

这篇关于如何声明constexpr extern?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 23:56
查看更多