gpt4 book ai didi

c++ - "Type is incomplete"(但不是)和代码编译

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:42:56 25 4
gpt4 key购买 nike

我有一个模板类

template<typename EventT, typename StateT, typename ActionT, bool InjectEvent = false, bool InjectStates = false, bool InjectMachine = false>
class StateMachine;

及其特化

template<typename EventT, typename StateT, typename ActionResultT, typename ...ActionArgsT, bool InjectEvent, bool InjectStates, bool InjectMachine>
class StateMachine<EventT, StateT, ActionResultT(ActionArgsT...), InjectEvent, InjectStates, InjectMachine>

特化用于将函数类型解析为其返回类型和参数类型。

类的实现按预期工作并且通过了所有测试。

如果我通过 ActionT = void()ActionT 添加默认值,Visual Studio 会提示“type StateMachine<...> is incomplete”并且IntelliSense 停止工作(至少对于这种类型的所有实例)。然而,代码编译并且所有测试都像以前一样通过(我还有一个显式使用默认参数的测试)。

这是 Visual Studio 中的错误还是我错过了什么?

我正在使用 VS 2015 Pro 和 C++ 14。

编辑

这是一个最小的工作示例:

#include <iostream>
#include <functional>

using namespace std;

template<typename EventT, typename StateT, typename ActionT = void(), bool InjectEvent = false, bool InjectStates = false, bool InjectMachine = false>
class StateMachine;

template<typename EventT, typename StateT, typename ActionResultT, typename ...ActionArgsT, bool InjectEvent, bool InjectStates, bool InjectMachine>
class StateMachine<EventT, StateT, ActionResultT(ActionArgsT...), InjectEvent, InjectStates, InjectMachine>
{
public:
typedef ActionResultT ActionT(ActionArgsT...);

StateMachine(ActionT&& action) : _action(action)
{
}

ActionResultT operator()(ActionArgsT... args)
{
return _action(args...);
}

void sayHello() const
{
cout << "hello" << endl;
}

private:
function<ActionT> _action;
};

int sum(int a, int b)
{
return a + b;
}

void print()
{
cout << "hello world" << endl;
}

void main()
{
StateMachine<string, int, int(int, int)> sm1(sum);
sm1.sayHello();
cout << sm1(2, 5) << endl;
StateMachine<string, int> sm2(print);
sm2();
sm2.sayHello();
getchar();
}

IntelliSense 抛出此错误:

对于 sm1,它找到成员函数 sayHello()...

但不适用于 sm2

但是代码编译并产生这个输出:

hello
7
hello world
hello

这是正确的。

最佳答案

我终于想通了,这是Resharper的intellisense的问题。如果我禁用 Resharper,代码将不再带有下划线。我会将此报告给 JetBrains 并让您了解最新信息。

编辑

万恶之源是将函数类型替换为函数签名:

template<typename FT = void()>
struct Func;

template<typename FR, typename ...FArgs>
struct Func<FR(FArgs...)>
{
// ...
}

更新

我在 youtrack 开了一张票(JetBrain 的问题跟踪器)并且已为其分配了一名开发人员。

关于c++ - "Type is incomplete"(但不是)和代码编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42233337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com