2012-11-19 99 views
1

我有一個非可複製(從boost::noncopyable繼承)類,我用它作爲自定義名稱空間。另外,我還有一個類,使用前一個,如下所示:模板類中的不可複製靜態常量成員類

#include <boost/utility.hpp> 
#include <cmath> 

template< typename F > 
struct custom_namespace 
    : boost::noncopyable 
{ 

    F sqrt_of_half(F const & x) const 
    { 
     using std::sqrt; 
     return sqrt(x/F(2.0L)); 
    } 

    // ... maybe others are not so dummy const/constexpr methods 

}; 

template< typename F > 
class custom_namespace_user 
{ 

    static 
    ::custom_namespace<F> const custom_namespace_; 

public : 

    F poisson() const 
    { 
     return custom_namespace_.sqrt_of_half(M_PI); 
    } 

    static 
    F square_diagonal(F const & a) 
    { 
     return a * custom_namespace_.sqrt_of_half(1.0L); 
    } 

}; 

template< typename F > 
::custom_namespace<F> const custom_namespace_user<F>::custom_namespace_(); 

這個代碼將導致下一個錯誤(即使沒有實例化):

錯誤:沒有「常量custom_namespace custom_namespace_user :: custom_namespace_user '

下一個方式custom_namespace_()」類中聲明成員函數' 是不合法的:

template< typename F > 
::custom_namespace<F> const custom_namespace_user<F>::custom_namespace_ = ::custom_namespace<F>(); 

我應該如何聲明這兩個類(首先是第二個不可複製的靜態常量成員類)?這是否容易?

+0

你爲什麼不只是只使用靜態成員函數?是否真的需要一個實例? – Pubby

+0

@ahenderson是的,但是'custom_namespace'有額外的'typedefs'和其他依賴於'F'的特性。 – Orient

+0

@Pubby這個無關緊要的東西。對此的回答超出了本次討論的範圍。 – Orient

回答

3

您的代碼被解析爲函數聲明,而不是對象定義。

解決的辦法是擺脫括號:

template< typename F > ::custom_namespace<F> const custom_namespace_user<F>::custom_namespace_;