启用和访问模板方法 [英] Enabling and accessing template method

查看:101
本文介绍了启用和访问模板方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中:

  #include< iostream> 
#include< cstdlib>
#include< type_traits>
使用std :: enable_if;
使用std :: is_same;
使用std :: cout;
使用std :: endl;

模板< typename T>
struct S {
S(){t = static_cast< T>(NULL);}
模板< typename U,typename enable_if< is_same< T,U> :: value> ::类型> void operator()(U& rrU){t = rrU;}

T t;
};

int main(){

S< int> S;
return(0);
}

如何实例化并使用运算符()在 main()

我必须承认,我不太清楚你试图达到什么目标,但是类型将被推断出来 / strong>,因此您不必担心这一点。无论如何,我会按照以下方式实施您的示例(并基于我对您的意图的理解):

  / ** @ file:test.cpp * / 
#include< utility>
#include< type_traits>

模板< typename T>
struct S {
S():t(0){};

模板< typename U>
void operator()(U& rrU){
static_assert(std :: is_same< T,U> value,typename U必须等于typename T);
t = rrU;
}

T t;
};

int main(){
S< int> S;

int a = 10;
s(std :: move(a));
#ifdef RAISES_ERROR
double b = 10.0;
s(std :: move(b));
#endif
return(0);
}

这可以通过两种方式进行编译:

  g ++ -std = c ++ 11 test.cpp 

编译得很好,第二种情况是引发编译错误(这就是你想要的),但是错误可以不言自明:

  g ++ -std = c ++ 11 -DRAISES_ERROR test.cpp 

test.cpp:在实例化'void S< ; T> :: operator()(U&& amp;)[with U = double; T = int]':
test.cpp:26:17:从这里需要
test.cpp:12:5:error:static assertion failed:Error
static_assert(std :: is_same< ; T,U> :: value,typename U必须等于typename T);
^


In the following code:

#include<iostream>
#include<cstdlib>
#include<type_traits>
using std::enable_if;
using std::is_same;
using std::cout;
using std::endl;

template<typename T>
struct S{
        S(){t = static_cast<T>(NULL);}
        template<typename U,typename enable_if<is_same<T,U>::value>::type>void operator()(U&& rrU){t = rrU;}

        T t;
};

int main(){

        S<int> s;
        return(0);
}

how could I instantiate and use the operator()template method defined within the template struct, after the instantiation of the template struct in main()?

解决方案

I have to admit that I'm not quite sure what you are trying to achieve, but the type will be deduced during compilation, thus you don't have to worry about that. Anyway, I would implement your example as follows (and it is based on my understanding of your intentions):

/** @file: test.cpp */
#include <utility>
#include <type_traits>

template <typename T>
struct S {
  S() : t(0)  { };

  template <typename U>
  void operator()(U &&rrU) {
    static_assert(std::is_same<T,U>::value, "typename U must be equal to typename T"); 
    t = rrU; 
  }

  T t;
};

int main() {
  S<int> s;

  int a = 10;
  s(std::move(a));
#ifdef RAISES_ERROR
  double b = 10.0;
  s(std::move(b));
#endif
  return (0);
}

This can be compiled in two ways:

g++ -std=c++11 test.cpp

that compiles fine, and a second case that raises a compilation error (is that what you want?), but the error can be made self-explanatory:

g++ -std=c++11 -DRAISES_ERROR test.cpp

test.cpp: In instantiation of ‘void S<T>::operator()(U&&) [with U = double; T = int]’:
test.cpp:26:17:   required from here
test.cpp:12:5: error: static assertion failed: Error
     static_assert(std::is_same<T,U>::value, "typename U must be equal to typename T");
     ^

这篇关于启用和访问模板方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆