我是一个c ++新手.今天,我有一个问题:在头文件中,我定义了一个类:
template<class T> class Ptr_to_const {
private:
Array_Data<T>* ap;
unsigned sub;
public:
...
Ptr_to_const<T> & operator=(const Ptr_to_const<T> & p);
};
Run Code Online (Sandbox Code Playgroud)
在源文件中,我编程为:
template<class T> Ptr_to_const<T>& Ptr_to_const<T>::operator=(
const Ptr_to_const<T> & p) {
...
return *this;
}
Run Code Online (Sandbox Code Playgroud)
编译时,编译器总是说:" 未找到成员声明 ". 为什么?
我使用eclipse CDT + Cygwin GCC
非常感谢你!
lsof test在 Mac OS X 上运行时得到以下输出,其中test是内存映射文件的名称:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
filesyste 40535 buildbot txt REG 1,6 3 2466028 test
Run Code Online (Sandbox Code Playgroud)
请注意,这txt是在FD现场报告的。但是,根据文档lsof:
FD is the File Descriptor number of the file or:
mem memory-mapped file;
txt program text (code and data);
Run Code Online (Sandbox Code Playgroud)
那么问题来了:为什么是lsof报道txt而不是mem现场FD?
该文件映射如下:
mmap(0, length, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0)
Run Code Online (Sandbox Code Playgroud)
其中length是长度,fd是文件的描述符。
我遇到了将浮点数转换为字符串的问题,这to_string对我来说太慢了,因为我的数据可能涉及数百万个浮点数。
我已经有了如何快速写出这些数据的解决方案。
然而,在解决了那个问题之后,我很快意识到浮点数到字符串的转换产生了很大的影响。
那么,除了使用其他非标准库之外,还有其他想法或解决方案吗?
这是对这个问题的跟进.答案表明存在
将Process out,err和输入流复制到System版本
用IOUtils.copy如下(后定影各种编译错误):
import org.apache.commons.io.IOUtils;
import java.io.IOException;
public class Test {
public static void main(String[] args)
throws IOException, InterruptedException {
final Process process = Runtime.getRuntime().exec("/bin/sh -i");
new Thread(new Runnable() {public void run() {
try {
IOUtils.copy(process.getInputStream(), System.out);
} catch (IOException e) {}
} } ).start();
new Thread(new Runnable() {public void run() {
try {
IOUtils.copy(process.getErrorStream(), System.err);
} catch (IOException e) {}
} } ).start();
new Thread(new Runnable() {public void run() {
try {
IOUtils.copy(System.in, process.getOutputStream()); …Run Code Online (Sandbox Code Playgroud) 我正在设计一个C API,除此之外还必须提供一种设置一些双值选项的方法.要识别选项,我使用以下枚举:
typedef enum
{
OptionA,
OptionB,
...
} Option;
Run Code Online (Sandbox Code Playgroud)
Option在公共API函数中用作参数类型是一个好主意:
int set_option(Option opt, double value);
Run Code Online (Sandbox Code Playgroud)
或者更好地使用int:
int set_option(int opt, double value);
Run Code Online (Sandbox Code Playgroud)
考虑到我将来可能需要添加更多选项?
还有现有API的任何一个很好的例子来证明这两种方法吗?
请考虑以下代码:
class A {
A(const A&);
public:
A() {}
};
int main() {
const A &a = A();
}
Run Code Online (Sandbox Code Playgroud)
此代码与GCC 4.7.2编译良好,但无法使用Visual C++ 2010进行编译,并出现以下错误:
test.cc(8) : error C2248: 'A::A' : cannot access private member declared in class 'A'
test.cc(2) : see declaration of 'A::A'
test.cc(1) : see declaration of 'A'
Run Code Online (Sandbox Code Playgroud)
因此,在将临时绑定到引用时,是否有必要使用复制构造函数?
这与我之前的问题有些相关:
我想做多个版本的文档,这些版本在包含的部分中有所不同.为此,我通常使用唯一的指令或ifconfig扩展名.但是,我不能将任何与toctree指令结合使用.
我基本上想要的是这样的:
.. toctree::
:maxdepth: 2
intro
strings
datatypes
numeric
.. only:: university
complex
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?
我想在 C++ 中创建一个简单的日志函数,它将代码位置添加到日志消息中。我想避免使用宏以及__FILE__&的使用__LINE__。
请注意,该format字符串始终是编译时字符串,我希望在编译时进行尽可能多的计算(目标机器是一个小型 MCU)。
我可以source_location通过experimental/source_location. 我也可以使用{fmt}。
我从这个开始。目前,我有以下几点:
#include <fmt/format.h>
#include <experimental/source_location>
using source_location = std::experimental::source_location;
void vlog(fmt::string_view format, fmt::format_args args)
{
fmt::vprint(format, args);
}
template <typename S, typename... Args>
void log(const S& format, const source_location& location, Args&&... args)
{
vlog(format, fmt::make_args_checked<fmt::string_view, uint32_t, Args...>(format, location.file_name(), location.line(), args...));
}
#define MY_LOG(format, ...) log(FMT_STRING("{},{}, " format), source_location::current(), __VA_ARGS__)
int main() {
MY_LOG("invalid squishiness: {}", 42);
}
Run Code Online (Sandbox Code Playgroud)
哪个产量正确./example.cpp,20, …
我广泛使用 fmtlib 中的 fmt::format 。将 Visual Studio 2022 从 17.3.5 更新到 17.4.0 后,我在使用代码发布版本时遇到如下问题:
\nconst std::string s1 = format("{}.{}", "abc", "test" );\nRun Code Online (Sandbox Code Playgroud)\n虽然 s1 和 s1.c_str() 在调试版本中是“abc.test”,但在使用 fmtlib 的发布版本中,只有 s1 是“abc.test”,并且 s1.c_str() 返回“abc.test”,后跟一些垃圾(例如“ abc.test\xc3\xb0_\xc3\xa0\xc3\xb4")。
\n我写了一个小测试程序:
\n#include <iostream>\n#include <string>\n#ifdef USE_STD_FORMAT\n#include <format>\nusing std::format;\n#else\n#include <fmt/format.h>\nusing fmt::format;\n#endif\n\nvoid check(const std::string& s, const std::string& expected)\n{\n if (s != expected)\n std::cout << "FAILED I: '" << s << "' != '" << expected << "'\\n";\n else if (strcmp(s.c_str(), expected.c_str()) != 0)\n std::cout << "FAILED …Run Code Online (Sandbox Code Playgroud) 考虑以下示例:
struct A {
using type = int;
};
template <typename T>
using B = A;
template <typename T>
typename B<T>::type f() { return {}; }
template B<int>::type f<int>();
Run Code Online (Sandbox Code Playgroud)
Clang 生成符号,int f<int>()而 GCC 生成B::type f<int>()实例化:https : //godbolt.org/z/MCCza4
为什么不同意的编译器,不应该GCC还决心B::type要int?