小编nic*_*225的帖子

了解 GCC 中的 std::pow 实现

我试图了解 std::pow 函数在 GCC 中的工作原理。我在这里查看 cmath 库的源代码https://code.woboq.org/gcc/libstdc++-v3/include/c_global/cmath.html#_ZSt3powff

在该文件的第 388 行,当 float 类型作为参数传递时,它们给出了 pow 的定义,如下所示

#ifndef __CORRECT_ISO_CPP_MATH_H_PROTO
  inline _GLIBCXX_CONSTEXPR float
  pow(float __x, float __y)
  { return __builtin_powf(__x, __y); }
Run Code Online (Sandbox Code Playgroud)

该 pow 函数调用另一个函数__builtin_powf

我想知道的是,__builtin_powf定义在哪里?我读到它是一个内置函数,但这仍然无法帮助我理解它的实现所在。我似乎无法在 GCC 源代码的其他任何地方找到__builtin_powf 。另外,它是如何翻译成汇编的?

c++ gcc built-in

7
推荐指数
1
解决办法
2365
查看次数

这是维护大型 C++ 项目的良好目录结构吗?

我一直在尝试找出一个适合大型 C++ 项目维护的良好目录结构。在我的搜索过程中,我在这里发现了这个资源链接。如果我松散地遵循该文档中所述的结构,似乎我会得到类似于以下内容的内容

\n\n
.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 CMakeLists.txt\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 build\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Executable-OutputA\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 Library-OutputA\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 cmake\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 *.cmake\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 docs\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 include\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 *.h\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 lib\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 LibraryA\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 *.cpp\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 *.h\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 LibraryB\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 *.cpp\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 *.h\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 ExecutableA\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 *.cpp\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 *.h\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 ExecutableB\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 *.cpp\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 *.h\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 tests\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 third_party\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 External-ProjectA\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 External-ProjectB\n
Run Code Online (Sandbox Code Playgroud)\n\n
    \n
  • build:保存项目生成的输出可执行文件和库
  • \n
  • cmake:保存项目可能需要的所有cmake包
  • \n
  • doc:保存文档文件,通常是 doxygen
  • \n
  • include:保存公共头文件(可能不需要,不确定)
  • \n
  • lib:保存用户使用各自的源文件和头文件创建的所有库
  • \n
  • src:保存用户使用各自的头文件和源文件创建的所有可执行项目
  • \n
  • 测试:用于测试可执行文件和库的文件
  • \n …

c++ directory-structure cmake

5
推荐指数
0
解决办法
3118
查看次数

从 docker 容器内连接到本地主机 postgres 数据库

我有一个linux postgres数据库在我的本地主机上运行,​​端口为5432。我的机器网卡的IP是192.168.68.80。我的 postgres 配置文件如下所示:

/etc/postgresql/13/main/postgresql.conf

listen_addresses = '*'
Run Code Online (Sandbox Code Playgroud)

/etc/postgresql/13/main/pg_hba.conf

host    all             all             0.0.0.0/0               md5
host    all             all             ::/0                    md5
Run Code Online (Sandbox Code Playgroud)

现在我想从 Docker 容器内运行的 Node 应用程序连接到该数据库。这是我的环境变量文件加载到 Node 时的样子:

.env

PGHOST=localhost // Host of the postgres db server
PGPORT=5432 // Port of the postgres db
Run Code Online (Sandbox Code Playgroud)

完成此设置后,我使用 docker 文件构建 docker 映像。然后,我尝试在 Linux 上使用以下命令将 dockerfile 作为容器运行:

sudo docker run --network="host" --env-file .env -p 3000:3000 repo:tag
Run Code Online (Sandbox Code Playgroud)

该应用程序开始运行,并侦听端口 3000 上的连接。但是每当我尝试连接到该应用程序时,该应用程序都无法连接到 postgres 数据库服务器并给出以下错误:

/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:172
                reject(new sequelizeErrors.ConnectionRefusedError(err));
                       ^

ConnectionRefusedError [SequelizeConnectionRefusedError]: connect ECONNREFUSED 127.0.0.1:5432
    at Client._connectionCallback (/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:172:24)
    at Client._handleErrorWhileConnecting (/node_modules/pg/lib/client.js:305:19) …
Run Code Online (Sandbox Code Playgroud)

postgresql networking node.js docker

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

隐式调用复制构造函数?

我有以下类,其中定义了普通构造函数和复制构造函数。

\n
#include <iostream>\n\nclass Bla\n{\npublic:\n    Bla()\n    {\n        std::cout << "Normal Constructor Called\\n";\n    }\n\n    Bla(const Bla& other)\n    {\n        std::cout << "Copy Constructor Called\\n";\n    }\n\n};\n\nint main() \n{\n    Bla a = Bla(); // prints Normal Constructor\n}\n
Run Code Online (Sandbox Code Playgroud)\n

在主函数中,它按照我的预期打印普通构造函数,并且仅打印普通构造函数。但是,如果我将复制构造函数设为类的私有成员,编译器会给出错误

\n
error: \xe2\x80\x98Bla::Bla(const Bla&)\xe2\x80\x99 is private within this context\n
Run Code Online (Sandbox Code Playgroud)\n

从表面上看,似乎调用了复制构造函数,但我没有看到从中打印任何内容。复制构造函数是否被隐式调用?这里发生了什么?

\n

c++ constructor copy-constructor copy-elision

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

C++ Singleton 私有构造函数无法从静态函数访问

我这里有一个单例类声明:

\n
#ifndef GLFW_CONTEXT_H\n#define GLFW_CONTEXT_H\n\n#include <memory>\n\nclass GLFWContextSingleton\n{\npublic:\n    static std::shared_ptr<GLFWContextSingleton> GetInstance();\n    ~GLFWContextSingleton();\n    GLFWContextSingleton(const GLFWContextSingleton& other) = delete;\n    GLFWContextSingleton* operator=(const GLFWContextSingleton* other) = delete;\n    \nprivate:\n    GLFWContextSingleton();\n};\n\n#endif\n
Run Code Online (Sandbox Code Playgroud)\n

GetInstance以及此处所示函数的实现

\n
std::shared_ptr<GLFWContextSingleton> GLFWContextSingleton::GetInstance()\n{\n    static std::weak_ptr<GLFWContextSingleton> weak_singleton_instance;\n    auto singleton_instance = weak_singleton_instance.lock();\n\n    if (singleton_instance == nullptr)\n    {\n        singleton_instance = std::make_shared<GLFWContextSingleton>();\n        weak_singleton_instance = singleton_instance;\n    }\n\n    return singleton_instance;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

然而,调用std::make_shared<GLFWContextSingleton>()给我一个错误说

\n
\xe2\x80\x98GLFWContextSingleton::GLFWContextSingleton()\xe2\x80\x99 is private within this context\n
Run Code Online (Sandbox Code Playgroud)\n

我认为这个静态方法可以访问私有成员函数。造成这种情况的原因是什么以及如何解决?

\n

c++ singleton

3
推荐指数
1
解决办法
960
查看次数

确定模板中模板参数的类型

我正在尝试用 C++ 实现一个通用链表和链表迭代器。我有一个节点结构如下

template <typename T>
struct Node
{
    T m_data;
    Node<T>* m_next;
};
Run Code Online (Sandbox Code Playgroud)

我还有一个链表迭代器,它是一个模板,因此它可以生成常规和const 迭代器。

template <typename NodeType>
class LinkedListIterator
{  
private:
    NodeType* m_node;
public:
    LinkedListIterator(NodeType* n);
    T& operator*() const;
};
Run Code Online (Sandbox Code Playgroud)

我的问题是如何正确声明operator*()函数?我的期望是像下面这样的东西应该工作

LinkedListIterator<const Node<T>> my_iter(some_node_pointer);
*my_iter = new_value; // should not work
Run Code Online (Sandbox Code Playgroud)

据我所知,在返回Toperator*()没有意义,因为这个类没有访问的类型名称Node类。

我通过为Node类中的类型创建别名找到了一种解决方法,如下所示

template <typename T>
struct Node
{
    typedef T type_value;
    // rest of Node class...
};
Run Code Online (Sandbox Code Playgroud)

现在我可以在我的迭代器类中执行以下操作

template <typename NodeType>
class LinkedListIterator
{
public:
    typename …
Run Code Online (Sandbox Code Playgroud)

c++ templates iterator typename

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