Skip to content

GCC compilation error and some improvement suggestions #490

Description

@PlumBlossomMaid

Bug category

  • bug - compilation error

Describe the bug

Two template functions in source/matplot/core/axes_type.h fail to compile with GCC due to incorrect template argument counts.
source/matplot/core/axes_type.h 中两个模板函数因模板参数数量错误,无法在 GCC 下编译。

Bug 1: graph() — Extra template argument (~line 2584)
Bug 1: graph() — 多余的模板参数(~第2584行)

to_vector_1d only accepts one template parameter. The extra std::pair<size_t, size_t> causes GCC to reject the call.
to_vector_1d 只接受一个模板参数。多余的 std::pair<size_t, size_t> 导致 GCC 拒绝编译。

// Current:(当前代码,GCC 拒绝)
return graph(to_vector_1d<T1, std::pair<size_t, size_t>>(edges), ...);
// Fix:(修复)
return graph(to_vector_1d<T1>(edges), ...);

Bug 2: imshow() — Extra template argument (~line 2596)
Bug 2: imshow() — 多余的模板参数(~第2596行)

to_vector_2d only accepts one template parameter. The original code tried to use to_vector_2d<T1, unsigned char> to convert data to unsigned char before calling the non-template imshow overload (which is designed for grayscale images). This intent is correct, but to_vector_2d doesn't support a second template parameter for the target type.
to_vector_2d 只接受一个模板参数。原始代码试图用 to_vector_2d<T1, unsigned char> 将数据转换为 unsigned char 后调用非模板 imshow 重载(该重载专为灰度图设计)。这个意图是正确的,但 to_vector_2d 不支持第二个模板参数来指定目标类型。

// Current:(当前代码,GCC 拒绝)
return imshow(to_vector_2d<T1, unsigned char>(gray_scale_img));

The correct fix depends on the intended API design:
正确的修复取决于 API 设计意图:

Option A — Add a two-parameter to_vector_2d that supports type conversion:
方案 A — 添加支持类型转换的双参数 to_vector_2d

template <class T, class U>
auto to_vector_2d(const T &v) {
    // Convert each element to type U
    // ...
}

Option B — Explicitly convert to unsigned char in the template:
方案 B — 在模板中显式转换为 unsigned char

template <class T1>
matrix_handle imshow(const IterableIterables<T1> &gray_scale_img) {
    auto v = to_vector_2d<T1>(gray_scale_img);
    std::vector<std::vector<unsigned char>> uc(v.size());
    for (size_t i = 0; i < v.size(); i++) {
        uc[i].resize(v[i].size());
        for (size_t j = 0; j < v[i].size(); j++) {
            uc[i][j] = static_cast<unsigned char>(std::clamp(v[i][j], 0.0, 255.0));
        }
    }
    return imshow(uc);
}

The same issue exists in the RGB imshow overload (~line 2606) — all four channels use to_vector_2d<T1, unsigned char> with the wrong template argument count, and all use T1 instead of T1, T2, T3, T4.
RGB 版 imshow 重载(~第2606行)也有同样的问题——四个通道都用了 to_vector_2d<T1, unsigned char>,模板参数数量错误,且全部使用 T1 而非 T1, T2, T3, T4

Steps to Reproduce

git clone https://github.com/alandefreitas/matplotplusplus.git
cd matplotplusplus
git checkout v1.2.1
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DMATPLOTPP_BUILD_EXAMPLES=OFF -DMATPLOTPP_BUILD_TESTS=OFF
cmake --build . -j$(nproc)

Then compile the following minimal reproduction file:
然后编译以下最小复现文件:

// test_reproduce.cpp
#include <matplot/matplot.h>

void test_graph() {
    std::vector<std::vector<size_t>> edges = {{0,1},{1,2}};
    matplot::gca()->graph(edges);
}

void test_imshow() {
    std::vector<std::vector<double>> data = {{1,2,3},{4,5,6},{7,8,9}};
    matplot::imshow(data);
}

int main() { return 0; }
g++ -std=c++17 -I source -I build/source/matplot -c test_reproduce.cpp -o /dev/null

Output

Details
test_reproduce.cpp:5:26: error: no matching function for call to 'matplot::axes_type::graph(std::vector<std::vector<long unsigned int> >&)'
source/matplot/core/axes_type.h:2580:24: note: candidate: 'template<class T1, class T2> ... graph(IterableValues<T1>&, IterableValues<T2>&, ...)'
source/matplot/core/axes_type.h:2580:24: note:   template argument deduction/substitution failed:
test_reproduce.cpp:5:26: note:   couldn't deduce template parameter 'T2'

source/matplot/core/axes_type.h:2597:58: error: no matching function for call to 'to_vector_2d<..., unsigned char>(...)'
source/matplot/core/axes_type.h:2597:58: error: wrong number of template arguments (2, should be 1)

Platform

  • cross-platform issue - linux

Environment Details:

  • OS: Ubuntu
  • OS Version: 22.04, 24.04
  • Compiler: GCC
  • Compiler version: 11.4, 12.3, 13.3

Additional context

We maintain a combined patch for these issues in our project Insight7 (https://github.com/PlumBlossomMaid/Insight7), a C++ scientific computing framework inspired by NumPy/SciPy/matplotlib/PaddlePaddle. Matplot++ is our primary plotting backend.
我们在自己的项目 Insight7(https://github.com/PlumBlossomMaid/Insight7 ,受 NumPy/SciPy/matplotlib/PaddlePaddle 启发的 C++ 科学计算框架)中维护了这些问题的合并补丁。Matplot++ 是我们的主要绘图后端。

The full diff is available at patches/matplotplusplus/gcc-compat.patch.
完整 diff 见 patches/matplotplusplus/gcc-compat.patch

We plan to contribute to Matplot++ around the end of 2026.
我们计划在 2026 年底左右开始为 Matplot++ 贡献代码。

We also have improvement suggestions:
我们还有一些改进建议:

  1. CI: actions/upload-artifact@v3 shut down since April 2026, upgrade to v4.
    CI: actions/upload-artifact@v3 自 2026 年 4 月起已停用,请升级到 v4。

  2. Chinese README: A README.zh-CN.md would significantly improve adoption in the large Chinese C++ community.
    中文 README: 添加 README.zh-CN.md 可显著提升项目在中国 C++ 社区的采用率。

  3. Alternative backend: Exploring AGG (Anti-Grain Geometry) as a gnuplot replacement would enable zero-dependency deployment.
    替代后端: 探索 AGG(Anti-Grain Geometry)替代 gnuplot,可实现零依赖部署。

  4. AI-assisted development: Adding a QWEN.md project configuration file would help AI coding assistants Qwen Code automatically understand the project's architecture, coding conventions, and technical decisions. The QWEN.md file acts as "a project manual for AI" — when an AI assistant starts a new session, it reads this file and immediately understands the project context, reducing repetitive explanations and improving code generation quality.
    AI 辅助开发: 添加 QWEN.md 项目配置文件,让 AI 编程助手 Qwen Code 自动理解项目的架构、编码规范和技术决策。QWEN.md 相当于"给 AI 看的项目说明书"——AI 助手开启新会话时会自动读取该文件,立即理解项目上下文,减少重复解释,提升代码生成质量。

    Additionally, creating .qwen/skills/ directory with reusable workflow patterns (e.g., how to add a new plot function, how to test with gnuplot) would further improve AI-assisted development efficiency.
    此外,创建 .qwen/skills/ 目录存放可复用的工作流模式(例如如何添加新的绘图函数、如何用 gnuplot 测试),可以进一步提升 AI 辅助开发的效率。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions