0

I'm using clang on mac and having:

$cat my.cpp
#include<type_traits>
using namespace std;
template<int i>
struct place_holder{};
place_holder<1> _1;
place_holder<2> _2;
template<typename T>
struct is_placeholder:public integral_constant<int,0>{};
template<int i>
struct is_placeholder<place_holder<i> >:public integral_constant<int,i>{};
template<typename T>
int check(T&& t){
    return is_placeholder<t>()::value;
}
int main(){
    int i=check<_1>();
    return 0;
}

The last line failed to compile:

my.cpp:13:27: error: template argument for template type parameter must be a type
    return is_placeholder<t>()::value;
                        ^
my.cpp:7:19: note: template parameter is declared here
template<typename T>
                ^
my.cpp:16:11: error: no matching function for call to 'check'
    int i=check<_1>();
        ^~~~~~~~~
my.cpp:12:5: note: candidate function template not viable: requires single argument 't', but no arguments were
    provided
int check(T&& t){
    ^
2 errors generated.

Really odd, my "struct is_placeholder" is really a template, right? Why it say it's not?

How to correct my program? Thanks!

Hind Forsum
  • 9,717
  • 13
  • 63
  • 119

1 Answers1

2

Seems that you confuse types and objects a bit.

For example, you have made a mistake trying to instantiate a template with t, which is not a type:

return is_placeholder<t>()::value;
                    ^^^^^^
                     here

Also, you have defined check as a function which accepts a single argument but you provide it with no arguments during the call.

I suggest you doing the next:

  1. Make _1 and _2 types:

    using _1 = place_holder<1>;
    using _2 = place_holder<2>;
    
  2. Change check function as follows:

    template<typename T>
    auto check() {
        return is_placeholder<T>::value;
    }
    

Then use as:

auto i = check<_1>();

Here is a fixed version: WANDBOX

Edgar Rokjān
  • 17,245
  • 4
  • 40
  • 67