此代码在 G++ 上运行,但不在 Visual C++ 上运行。
#include <iostream>
template<typename T> void foo( T& t,int some_parameter){}
template decltype(foo<int>) foo;
int main(){
std::cout << "Hello, world!\n";
}
这是来自 Visual C++ 的错误:-
错误 C2206: 'foo': typedef 不能用于函数定义
激励:我不想为显式实例化重复函数签名。我从https://stackoverflow.com/a/28356212
修改了代码。
哪一个是错的?如何在 Visual C++ 中解决它?
当前的间接解决方法
一天后,这是我发现的最佳解决方法:https ://stackoverflow.com/a/50350144 。
#include <tuple>
template<typename... Ts>auto instantiate() {
static auto funcs = std::tuple_cat(std::make_tuple(
foo1<Ts>,
foo2<Ts>
)...);
return &funcs;
}
template auto instantiate<int, double>();
foo.cpp
但是,在 Visual C++ 中,它仅在打开优化进行编译时才有效:-
Custom
或者Disabled(/Od)
不行。- 全部使用
/O1
,/O2
就/Ox
OK了。 - 没有任何
/Od
,/O1
,/O2
和/Ox
:-- 刚刚
/Og
好。 - 只是
/Oi
,/Ot
,/Oy
,/Ob2
,/GF
and/Gy
是不行的。 - 使用上面两行中的所有标志就可以了。
- 刚刚
变通方法的变通方法(使用/Od
):std::tuple_size<decltype(instantiate<int, double>())>
在 .cpp 中调用内部虚拟函数。然后在头文件中声明虚拟函数。