我得到一个编译器错误铿锵++。 MSVC++很高兴。我相信我的声明是正确的。问题朋友模板函数铿锵++/msvC++和enable_if
我的信念不正确,我在MSVC中“幸运”? 是否有一个非#ifndef _MSC_VER ... public:
的方式使这两个编译器的工作?
我想保持构造函数为私有。真正的代码稍微复杂一些。 (额外的模板元编程和“完美转发”)下面是问题的蒸馏版本,并尽可能地隔离问题。我已经为朋友声明尝试了许多变体。示例中显示了“似乎最好”的那个。
#include<type_traits>
template<typename T> class Wrap;
template<typename T,
typename std::enable_if<std::is_class<T>::value, T>::type* = nullptr >
Wrap<T> make_wrapper(T i)
{
return Wrap<T>(i);
}
template<typename T>
class Wrap : T
{
friend Wrap<T> make_wrapper<T,nullptr>(T);
private:
Wrap(T v) : T(v) {}
};
template<typename T>
class Imp
{
T x;
public:
Imp(T v) {}
};
int main()
{
auto wrap = make_wrapper(Imp<int>(1));
return 0;
}
铛++:
$ clang++ --version
Debian clang version 3.5-1~exp1 (trunk) (based on LLVM 3.5)
Target: x86_64-pc-linux-gnu
Thread model: posix
$ clang++ -std=c++11 test.cpp
test.cpp:8 : 12 : error : calling a private constructor of class 'Wrap<Imp<int> >'
return Wrap<T>(i);
^
test.cpp:30 : 17 : note : in instantiation of function template specialization 'make_wrapper<Imp<int>, nullptr>' requested here
auto wrap = make_wrapper(Imp<int>(1));
^
test.cpp:16 : 5 : note : declared private here
Wrap(T v) : T(v) { }
^
1 error generated.
CL
Microsoft Visual Studio Professional 2013
Version 12.0.30501.00 Update 2
1>------ Build started: Project: Test1, Configuration: Debug Win32 ------
1> Test1.cpp
1> Test1.vcxproj -> C:\cygwin64\home\username\Test1\Debug\Test1.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
FWIW,g ++ 4.8.2认为你的代码是好的。 –
谢谢我应该检查g ++以及相关代码。如果我不让太多额外的怪癖渗透到代码中,我怀疑它将为我节省时间。到目前为止,我一直在避免花费太多时间。我想现在是时候将g ++添加到列表中。 – ErnieE