为什么 Eclipse 会抱怨 C++ 模板的这种使用? [重复]

问题描述 投票:0回答:1

在 Eclipse 中,我正在查看的代码与 codereview.SX 问题 “C++17:基于 Boost.Hana 的编译时插件注册”中发布的示例基本相同,只是压缩为一个文件并删除了 Boost 内容:

#include <iostream>
#include <type_traits>

template<class>
struct sfinae_true;

template<unsigned int>
struct registry_hook;

namespace registry {
  template<class Dummy, unsigned int testN>
  constexpr sfinae_true<decltype(registry_hook<testN>{})> test(int);
  template<class Dummy, unsigned int testN>
  constexpr std::false_type test(long);

  template<class Dummy, unsigned int testN = 0>
  constexpr unsigned int get_free_N() {
    if constexpr (std::is_same<decltype(test<Dummy, testN>(0)), std::false_type>::value) {
        return testN;
    } else {
        return get_free_N<Dummy, testN + 1>();
    }
  }
}

struct Dummy1;
template<>
struct registry_hook<registry::get_free_N<Dummy1>()> {
    constexpr static unsigned int my_N = registry::get_free_N<Dummy1>();
};

struct Dummy2;
template<>
struct registry_hook<registry::get_free_N<Dummy2>()> {
    constexpr static unsigned int my_N = registry::get_free_N<Dummy2>();
};


int main() {
    constexpr unsigned int max_N = registry::get_free_N<void>() - 1;
    std::cout << max_N << std::endl;
}

这可以在 GCC 和 clang 中编译并正常工作:

$ g++     -std=c++17 -Wall -Wextra -pedantic main.cc && ./a.out
1
$ clang++ -std=c++17 -Wall -Wextra -pedantic main.cc && ./a.out
1

但是,Eclipse 会抱怨具有显式模板特化的行,并给出错误“无效模板参数”。解析器日志文件显示相同的内容,没有任何更多上下文:

Project:               test
File:                  file:[…]/main.cc
Language:              GNU C++
Index Version:         220.0
Build Configuration:   Default
Context:               file:[…]/main.cc
   C++, {}
Versions in Index:     1
   C++: {}; 0 macros, 2 includes, 47 names;

Include Search Path (option -I):
   /usr/include/c++/11
   /usr/include/x86_64-linux-gnu/c++/11
   /usr/include/c++/11/backward
   /usr/lib/gcc/x86_64-linux-gnu/11/include
   /usr/local/include
   /usr/include/x86_64-linux-gnu
   /usr/include

Macro definitions (option -D):
   […]

Unresolved names:
   A template id provides illegal arguments for the instantiation: registry_hook in file […]/main.cc:28
   A template id provides illegal arguments for the instantiation: registry_hook in file […]/main.cc:34

Written on Tue Dec 05 23:01:27 EST 2023

我不明白问题是什么。这只是 Eclipse/CDT 中的一个错误吗?

c++ eclipse eclipse-cdt sfinae c++-templates
1个回答
0
投票

Eclipse 抱怨具有显式模板专业化的行,

明确的专业化没有任何问题,并且该计划格式良好

这只是 Eclipse/CDT 中的一个错误吗?

看起来像 eclipse/cdt 中的一个错误,因为它说“无效的模板参数”,这在显式专业化中是不正确的。

© www.soinside.com 2019 - 2024. All rights reserved.