英文:
I have a string which is used across different files and translation unit, should I use constexpr const char* or const std::string?
问题
在头文件中,如果要在不同的文件和翻译单元之间使用字符串变量,你应该将它存储为 const std::string
还是 constexpr const char*
呢?
这里有一个示例:
// config.h
constexpr const char* string_a = "stringA"; // <--我应该这样声明吗?
const std::string string_b = "stringB"; // <--还是这样?
// a.h
#include "config.h"
class A {
public:
A();
};
// a.cpp
#include "a.h"
A::A() {
cout << string_a << endl;
}
// b.h
#include "config.h"
class B {
public:
B();
};
// b.cpp
#include "b.h"
B::B() {
cout << string_b << endl;
}
// main.cpp
#include "a.h"
#include "b.h"
int main() {
A a;
B b;
cout << string_a << endl;
cout << string_b << endl;
return 0;
}
我从书籍中了解到,通常建议使用 std::string
而不是 char*
,但我也听说 constexpr
在编译时初始化,这是相当不错的。
英文:
If I have a string variable in a header file which will be used across different files and translation units, should I store it in a constexpr const char* or const std::string?
Here is an example:
// config.h
constexpr const char* string_a = "stringA"; // <--should i declare it like this?
const std::string string_b = "stringB"; // <--or like this?
// a.h
#include "config.h"
class A{
public:
A();
};
// a.cpp
#include "a.h"
A::A(){
cout << string_a << endl;
}
// b.h
#include "config.h"
class B{
public:
B();
};
// b.cpp
#include "b.h"
B::B(){
cout << string_b << endl;
}
// main.cpp
#include "a.h"
#include "b.h"
int main(){
A a;
B b;
cout << string_a << endl;
cout << string_b << endl;
return 0;
}
I have read from books that it is recommended to always use string instead of char*, but I have also heard that constexpr is initialized in compile time which is quite good.
答案1
得分: 1
任何时候您考虑在速度与可读性/可维护性之间进行权衡时,都必须对您的代码进行性能分析,以确定是否会看到非常小的速度提升。
对于您的情况,constexpr 不太可能提供显著的速度提升。显著的缺点是您将不得不管理 const char* 中的内存,并且将失去对 std::string 的方便功能的访问。
英文:
Any time you consider making a tradeoff between speed vs. readability/maintainability, it is vital to profile your code to determine whether or not you see a non-negligible speed increase.
For your case, it is unlikely that constexpr will provide a significant speed increase. The significant downside is that you will have to manage the memory in the const char*, and you will lose access to std::string's convenient features.
答案2
得分: 0
没有明确的答案,需要了解具体上下文才能回答这个问题。
在许多情况下,选择哪个选项可能对结果没有太大影响,因此您应该选择与您正在使用的代码库相匹配的选项(即考虑代码清晰度,让编译器负责优化)。
如果您需要使用std::string
类提供的函数,那么创建std::string
可能是一个好主意。
另一方面,如果您正在为资源有限的环境编写代码,例如具有少量RAM和存储在非易失性存储器中的嵌入式应用程序,那么编译器能否在编译时构造它可能很重要。您需要进行实验,但这肯定可以使用const char *
,但使用std::string
则不那么确定(尽管编译器一直在不断改进,所以情况可能会改变)。
编译器是否能够在编译时构造变量还会影响是否在头文件中定义字符串是否是一个好主意。这是另一个问题,参见https://stackoverflow.com/questions/12042549/define-constant-variables-in-c-header。
最后,如果您需要从不是用C ++编写的代码模块访问字符串,例如纯C模块,那么const char *
可能更可取。
英文:
There is no definitive answer to the question without knowing the specific context.
In many situations, it will not matter to the result which one you choose, and thus you should go for the one which fits the code base you are working with (i.e. think of code clarity and let the compiler worry about optimizing).
If you need to apply functions offered by the std::string
class, then creating the std::string
may be a good idea.
On the other hand, if you are coding for an environment with limited resources, e.g. an embedded application with a small amount of RAM and the program stored in non-volatile memory, it may be important that the the compiler is able to construct it at compile time. You would have to make the experiment, but this would certainly work with the const char *
, but not so certainly with the std::string
(though compilers get better all the time, so this may change).
Whether or not the compiler would be able to construct the variable at compile time also impacts whether or not it is a good idea to define the string in a header file at all. This is a question of its own, see https://stackoverflow.com/questions/12042549/define-constant-variables-in-c-header.
Finally, the const char *
would be preferable if you need to access the string from code modules not written in C++, e.g. a plain C module.
答案3
得分: 0
这取决于用途,例如,如果以后您想在某些功能中将const char用作std::string,那么它将具有将const char转换为std::string的开销。但我仍然会避免使用const char*,而是使用std::string_view,您可以将其标记为constexpr,但也可以将其转换为const char*或std::string,反之亦然。只需小心构造std::string_view的对象的生存周期。在这里,这不是问题,因为它是全局的。
英文:
It depends on the usage , for example if down the line you want to use the const char* as std::string in some functionality then it will have the overhead of creating the std::string out of const char*. But i would still move away from const char* and use std::string_view which you can constexpr but has the ability to tranform into const char* or std::string and vice-versa. Just be careful about the life time of object from which std::string_view is constructed. Here its not the problem as its gloabl.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论