Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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++ 强typedefs_C++_Templates_Type Deduction - Fatal编程技术网

C++ 强typedefs

C++ 强typedefs,c++,templates,type-deduction,C++,Templates,Type Deduction,有没有办法制作一个类型的完整副本,以便在模板演绎上下文中对其进行区分?例如: #include <iostream> template <typename T> struct test { static int c() { static int t = 0; return t++; } }; typedef int handle; int main() { std::cout << tes

有没有办法制作一个类型的完整副本,以便在模板演绎上下文中对其进行区分?例如:

#include <iostream>

template <typename T>
struct test
{
    static int c()
    { 
        static int t = 0;
        return t++;
    }
};

typedef int handle;

int main()
{
    std::cout << test<int>::c() << std::endl;
    std::cout << test<handle>::c() << std::endl;
    return 0;
}
#包括
模板
结构测试
{
静态int c()
{ 
静态int t=0;
返回t++;
}
};
typedef int句柄;
int main()
{
标准::coutQuoting

请注意,typedef和using都不能创建新的不同数据类型。 它们只创建现有类型的同义词。这意味着 上面用WORD类型声明的myword的 键入unsigned int;这并不重要,因为两者实际上都是 指同一类型的

由于
int
handle
是同一个,因此需要输出
01

不过,正如@interjay所说,还有一个变通办法

你可以用


如建议的那样增强\u STRONG\u TYPEDEF

template<typename>
struct test {
    static int c() {
        static int t = 0;
        return t++ ;
    } 
};

//Instead of 
//typedef int handle

BOOST_STRONG_TYPEDEF(int , handle) ;  

int main() {

    std::cout << test<int>::c() << std::endl
    std::cout << test<handle>::c() << std::endl ;
    return 0;
}
模板
结构测试{
静态int c(){
静态int t=0;
返回t++;
} 
};
//而不是
//typedef int句柄
BOOST_STRONG_TYPEDEF(int,handle);
int main(){

std::cout问题是找到一个替代
typedef
的方法,它不会有这种行为。语言中没有内置的,但这并不意味着无法构建(例如,我在上面发布的链接)。是的,我知道typedef没有提供所需的行为。链接看起来很有希望!BOOST_STRONG_typedef有两个问题。它不适用于用户定义的类型,并且在模板演绎中无法区分同一原语的多个typedef。我写了一个解决这些问题的答案。@shauryachats实际上我认为我错了我在某个地方问过了。返回到已接受!看一看。关于你现在删除的关于你的开源项目的问题,你可以在相关的Reddit社区上问这个问题。但是,如果你在多个子网站上问这个问题,请确保你声明了你的交叉发布,这样人们就可以避免重复回答。
template<typename>
struct test {
    static int c() {
        static int t = 0;
        return t++ ;
    } 
};

//Instead of 
//typedef int handle

BOOST_STRONG_TYPEDEF(int , handle) ;  

int main() {

    std::cout << test<int>::c() << std::endl
    std::cout << test<handle>::c() << std::endl ;
    return 0;
}
template<typename, unsigned int N>
struct test {
    static int c() {
        static int t = 0;
        return t++ ;
    }

};
int main() {

    std::cout << test<int, 0>::c() << std::endl ;
    std::cout << test<handle,1>::c() << std::endl ;
    return 0;
} 
#define DEFINE_TEST_TYPE(type) \
typedef test<type, __COUNTER__> test_##type;


template<typename, unsigned int N>
struct test {    
     static int c() {
        static int t = 0;
        return t++ ;   
    }
};

typedef int handle ;

DEFINE_TEST_TYPE(int) ;
DEFINE_TEST_TYPE(handle) ;

int main() {
    std::cout << test_int::c() << std::endl ;
    std::cout << test_handle::c() << std::endl ;
    return 0;
}