似乎在 C++ 中,在静态内存中完全拥有字符串文字的唯一方法是将它们声明为:
class A
{
static const char const * = "Hello World";
};
Run Code Online (Sandbox Code Playgroud)
我注意到std::string没有常量表达式构造函数。我知道std::string实现基于动态分配,但为什么没有另一个符合std::string 的字符串类可以完成这项工作?
为了constexpr在编译时强制对函数求值,我应该能够将它的返回值赋给一个constexpr变量。
constexpr bool const_d_ref(const double& v) { return false; }
int main() {
constexpr double dd = 0.0;
constexpr bool cb = const_d_ref(dd);
}
Run Code Online (Sandbox Code Playgroud)
这似乎适用于g++和clang++。
为了对constexpr消费者隐藏,我将实际的函数定义移到 中namespace detail,创建一个新函数,该函数将返回值分配给一个constexpr变量并返回它。
namespace detail {
constexpr bool const_d_ref(const double& v) { return false; }
}
constexpr bool const_d_ref(const double& v) {
constexpr bool b = detail::const_d_ref(v);
return b;
}
int main() {
constexpr double dd = 0.0;
bool b = …Run Code Online (Sandbox Code Playgroud) 我目前正在研究更多 C++11 的东西,并在constexpr. 在我的一本书中,据说您应该将它用于常量,例如 ? 例如以这种方式:
#include <cmath>
// (...)
constexpr double PI = atan(1) * 4;
Run Code Online (Sandbox Code Playgroud)
现在我想把它放在一个自己的命名空间中,例如。MathC:
// config.h
#include <cmath>
namespace MathC {
constexpr double PI = atan(1) * 4;
// further declarations here
}
Run Code Online (Sandbox Code Playgroud)
...但这里 IntelliSense 说function call must have a constant value in a constant expression。
当我声明PI以下方式时,它有效:
static const double PI = atan(1) * 4;
Run Code Online (Sandbox Code Playgroud)
编译器似乎不喜欢constexpr但static const在这里的实际原因是什么?不constexpr应该在这里也有资格,或者这完全与这里的上下文有关并且constexpr不应该在函数之外声明?
谢谢你。
我有两个片段。
第一个片段:
#include <string>
template <typename T>
constexpr bool foo(T&&) {
return false;
}
int main() {
std::string a;
if constexpr (foo(a)) {
}
}
Run Code Online (Sandbox Code Playgroud)
第二个片段:
#include <string>
template <typename T>
constexpr bool foo(T&&) {
return false;
}
int main() {
std::string a;
std::string& x = a;
if constexpr (foo(x)) {
}
}
Run Code Online (Sandbox Code Playgroud)
第一个编译,但第二个不编译(错误消息:错误:'x' 的值在常量表达式中不可用。为什么?为什么a在常量表达式中可用,而x不是?
该命令,用于编译g++ -std=c++17 main.cpp.
我用 constexpr 函数在 C++ 中实现了 FNV-1A 函数,但遇到了一个奇怪的问题。
这个版本没有问题:
template<size_t N>
static constexpr uint32_t fnv1a_internal(const char (&data)[N], size_t position, uint32_t state) {
return position >= N - 1 ? state : fnv1a_internal(data, position + 1, (state ^ data[position]) * 16777619UL);
}
template<size_t N>
static constexpr uint32_t fnv1a(const char (&data)[N]) {
return fnv1a_internal(data, 0, 2166136261UL);
}
Run Code Online (Sandbox Code Playgroud)
但是当我将 position 设为 const 通用参数时,就像这样
template<size_t N, size_t Position>
static constexpr uint32_t fnv1a_internal(const char (&data)[N], uint32_t state) {
return Position >= N - 1 ? state …Run Code Online (Sandbox Code Playgroud) 我试图从另一个类中声明的数组中获取一些值。该数组具有固定长度和常量元素(我将 100% 从不修改其值,所以这就是我将其设为常量的原因)。
但是,当我尝试访问main函数中的第一个元素时,出现编译错误:
basavyr@Roberts-MacBook-Pro src % g++ -std=c++11 main.cc
Undefined symbols for architecture x86_64:
"Vectors::vec1", referenced from:
_main in main-c29f22.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1
Run Code Online (Sandbox Code Playgroud)
如您所见,我正在使用 clang(最新版本)在 macOS Catalina 上进行编译。
[问]:可能是什么问题?先感谢您。
这是代码:
#include <iostream>
class Dimension
{
public:
static constexpr int dim1 = 2;
static constexpr int dim2 = 1;
};
class Vectors
{
public:
static constexpr double vec1[2] = {4.20, 6.9};
};
int main()
{ …Run Code Online (Sandbox Code Playgroud) 我编写了一个可以在编译时初始化和复制的类,并且在我的函数之外使用该对象也可以在编译时工作。现在我想将我的constexpr对象传递给一个函数来做一些计算,但是编译器产生一个错误,我的对象不是一个常量表达式。
我必须如何编写函数才能使用我的constexpr对象?
Using GCC 9.2, C++17, (CLion option to use C++20已激活)
这是我的课程,我的功能和主要内容。注意:并不是所有的定义都给出了,因为它会为一篇文章写很多代码。
template<std::size_t t1_skipPos, std::size_t t2_skipPos, typename T1, typename T2>
constexpr auto contraction(T1 tensor1, T2 tensor2){
/*ERROR: tensor1 is not a constant expression*/
auto sris_tensor1 = save_recreated_index_sequence<0, tensor1.indices_amount-2,
t1_skipPos, tensor1.indices_amount, DIM3>(tensor1.calculate_indices());
return sris_tensor1;
}
template<typename T, typename ... Args>
class tensorBase{
private:
std::array<T, positive_natural_compiletime_pow<DIM3, std::tuple_size<Args...>::value>()> data;
public:
//std::vector<T> data = {};
std::tuple<Args...> myTypeTup;
std::size_t indices_amount = std::tuple_size<Args...>::value;
template<typename ... Element>
constexpr tensorBase(Element&&... input) : data{input...} {}; …Run Code Online (Sandbox Code Playgroud) 这是一个让我头疼的问题,但它适用于 const,但不适用于 constexpr,我想知道你们聪明的人是否可以解释一下。用 g++ -std=c++14 编译。
struct Service
{
std::string name;
enum {
thread,
interrupt,
end_types
} que_type;
};
const Service namedServices[] =
{
{"abc", Service::thread},
{"efg", Service::thread},
{"hij", Service::interrupt},
{"klm", Service::thread},
{"nop", Service::interrupt},
{"qrs", Service::thread},
{"", Service::end_types}
};
constexpr int thcnt()
{
int cnt = 0;
for (const Service* sp = namedServices; sp->que_type != Service::end_types; sp++) {
if (sp->que_type == Service::thread)
cnt++;
}
return cnt;
}
int main(int argc, char** argv)
{
std::string strs[thcnt()];
...
Run Code Online (Sandbox Code Playgroud)
这可以编译并且据我所知是正确的。如果我改为
constexpr Service namedServices[] …Run Code Online (Sandbox Code Playgroud) 我正在使用类型为 的变量itk::Image<OutputPixelType, Dimension>,其中“itk”来自图像处理库 ITK。
以下代码编译:
constexpr unsigned int Dimension = 3;
using PixelType = float;
using MyImageType = itk::Image<PixelType, Dimension>;
Run Code Online (Sandbox Code Playgroud)
但现在我需要将“维度”定义为从函数计算出来的东西。
unsigned int Dimension = get_dimension(...);
Run Code Online (Sandbox Code Playgroud)
我的编译器报错:
error: non-type template argument is not a constant expression
using MyImageType = itk::Image<PixelType, Dimension>;
^~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?我希望使用“维度”作为从函数计算出来的东西。
我想constexpr在一些代码中实现一些逻辑。我能够毫无问题地编译和执行此代码。
#include <iostream>
int main(){
std::cout << "Starting c++ main" << std::endl;
constexpr int val_1 = 100;
int val_2;
if constexpr (val_1 == 100){
val_2 = 10;
}
else if constexpr (val_1 == 200){
val_2 = 20;
}
std::cout << val_2 << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
但真的,val_2应该也是一个constexpr。我怎样才能val_2成为一个constexpr?我尝试了几件事,但没有成功。如果我声明val_2为constexpr,则不允许我在if语句中更改其值。如果我没有constexpr在if语句体之前声明变量,那么它(正如预期的那样)不是在if语句之外定义的。