Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c+中的部分专门化模板类+;_C++_C++11_Templates - Fatal编程技术网

C++ c+中的部分专门化模板类+;

C++ c+中的部分专门化模板类+;,c++,c++11,templates,C++,C++11,Templates,我有一个模板类: template <typename T> struct randimpl { // default is to not compile, have to pass T to something to get assert // evaluated during second phase expansion, is_void is an arbitrary choice. static_assert( std::is_void

我有一个模板类:

template <typename T> struct randimpl {
    // default is to not compile, have to pass T to something to get assert
    // evaluated during second phase expansion, is_void is an arbitrary choice.
    static_assert(
        std::is_void<T>::value && false, "random() not implemented for type"
    );
};
我试图对类型
\u point1d
进行部分专门化:

//生成随机实例\u点1d
模板
结构randimpl{
静态_point1drandom(){
返回_point1d(random());
}
};
据我所知,这是正确的语法,但当我编译时,我得到:

inc/geometry.h: In static member function ‘static _point1d<T> randimpl<_point1d<T> >::random()’:
inc/geometry.h:287:31: error: expected primary-expression before ‘(’ token
             return _point1d<T>(random<T>());
                               ^
inc/geometry.h:287:40: error: expected primary-expression before ‘>’ token
             return _point1d<T>(random<T>());
                                        ^
inc/geometry.h:287:42: error: expected primary-expression before ‘)’ token
             return _point1d<T>(random<T>());
inc/geometry.h:在静态成员函数“static _point1drandimpl::random()”中:
inc/geometry.h:287:31:错误:应在“(”标记之前使用主表达式
返回_point1d(random());
^
inc/geometry.h:287:40:错误:在“>”标记之前应该有主表达式
返回_point1d(random());
^
inc/geometry.h:287:42:错误:在“')标记之前应该有主表达式
返回_point1d(random());

我的语法错了吗?这似乎应该是可行的,但我不明白它为什么不接受它。

问题是
静态
成员函数的名称
随机
隐藏了全局函数,它试图调用自身(即递归调用)。但它不是一个模板,也不符合它的调用方式

您可以添加以指定全局
随机
,例如

// generate random instances _point1d<T>
template <typename T>
struct randimpl<_point1d<T>> {
    static _point1d<T> random() {
        return _point1d<T>(::random<T>());
        //                 ^^
    }
};
//生成随机实例\u点1d
模板
结构randimpl{
静态_point1drandom(){
返回_point1d(::random());
//                 ^^
}
};

返回点1d(::random())
static\u assert(std::is\u void::value&&false,”)
在技术上是UB
always\u false::value
是另一种选择。这是一个很好的捕获,我更改了它(请参阅我的帖子),但我得到了相同的错误消息。啊,别担心,random不在全局名称空间中,添加显式名称空间可以按预期修复它,谢谢!
inc/geometry.h: In static member function ‘static _point1d<T> randimpl<_point1d<T> >::random()’:
inc/geometry.h:287:31: error: expected primary-expression before ‘(’ token
             return _point1d<T>(random<T>());
                               ^
inc/geometry.h:287:40: error: expected primary-expression before ‘>’ token
             return _point1d<T>(random<T>());
                                        ^
inc/geometry.h:287:42: error: expected primary-expression before ‘)’ token
             return _point1d<T>(random<T>());
// generate random instances _point1d<T>
template <typename T>
struct randimpl<_point1d<T>> {
    static _point1d<T> random() {
        return _point1d<T>(::random<T>());
        //                 ^^
    }
};