2013-08-26 35 views
0

在以下上下文中,什麼是奇怪語言結構X< class Y > Y;在boost :: spirit :: x3中用作標籤的奇怪結構

#include <iostream> 
#include <sstream> 
#include <typeinfo> 
#include <type_traits> 
#include <cstdlib> 
#include <cxxabi.h> 

template< typename T > 
std::string const type_info_str() 
{ 
    int status = 0; 
    auto realname_(abi::__cxa_demangle(typeid(T).name(), nullptr, nullptr, &status)); 
    switch (status) { 
    case -1: return "Could not allocate memory"; 
    case -2: return "Invalid name under the C++ ABI mangling rules"; 
    case -3: return "Invalid argument to demangle"; 
    } 
    std::ostringstream oss; 
    if (std::is_volatile<T>::value) { 
     oss << "volatile "; 
    } 
    oss << realname_; 
    std::free(realname_); 
    if (std::is_const<T>::value) { 
     oss << " const"; 
    } 
    if (std::is_rvalue_reference<T>::value) { 
     oss << " &&"; 
    } else if (std::is_lvalue_reference<T>::value) { 
     oss << " &"; 
    } 
    return oss.str(); 
} 

template< typename T > 
struct X { }; 

int main() 
{ 
    X< class Y > Y; 
    std::cout << type_info_str< decltype(Y) >() << std::endl; // X<main::Y> 
    return EXIT_SUCCESS; 
} 

它的目的和語義是什麼?

+1

我會說它檢查'std :: ignore'的編譯時行爲。你從中得到了什麼樣的背景? –

+0

我什至不是。你的問題是什麼? –

+0

我的問題是關於'X < class Y > const Y;'。 – Orient

回答

3

讓我們來分析一下:

X <class Y> Y 
| ^^^^^^^^^ | 
|  |  | 
|  |  | 
|  |  ---> name of the variable 
|  | 
|  ---> the struct is a template which is instantiated with a type 
|   (in this case, the type is an incomplete class named Y) 
| 
---> struct declared globally 

如前所述Agent_L,有類名和因爲它可以從上下文推斷出你的真正用意是什麼變量之間沒有衝突:類型的變量或。

+0

那麼在尖括號中我們有一個類的前向聲明?就像在「Y類」中一樣; class X {X(Y *){;}}; class Y {};'? – Orient

+0

@Dukales是的,這是正確的 – user1233963

+0

我不能以除標籤之外的任何方式使用不完整的類型(編譯時功能),不是嗎? – Orient