【发布时间】:2017-07-03 04:00:27
【问题描述】:
我有一个模板类
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 并设置为 ActionT = void(),Visual Studio 会抱怨“类型 StateMachine<...> 不完整”并且 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
这是正确的。
【问题讨论】:
-
我不知道这是否对您有帮助,但您的代码在
g++和clang++上运行良好 -
它也适用于 msvc。我认为这是 IntelliSense 解析器的问题。
-
这个智能感知问题是否只发生在这个项目上?您可以尝试使用这些故障排除方法:1:关闭VS并卸载,然后重新加载解决方案,2:右键单击解决方案名称并选择“清洁解决方案”3:关闭VS并删除.suo文件或.csproj。您当前解决方案的用户文件(请先备份)并重新打开此解决方案。
-
在多个项目中不会发生这种情况。上面的代码来自一个只包含该代码的干净解决方案。我也尝试过卸载项目,删除 .suo 文件并清理项目。不幸的是,这些都没有解决我的问题。
标签: c++ visual-studio-2015 intellisense template-specialization